|
楼主 |
发表于 2003-6-15 23:47:13
|
显示全部楼层
因为题目是约定了只能修改DEPART类的.
我把程序改成了这样,还是通过不了- #include <iostream>
- #include <string>
- using namespace std;
- const int CODE_LEN=8;
- const int NAME_LEN=40;
- class DEPART{
- public:
- DEPART()
- {
- code=new char[CODE_LEN+1];
- strcpy(code,"");
- name=new char[NAME_LEN+1];
- strcpy(name,"");
- //cout<<"D_\n";
- return;
- }
- DEPART(char* dep_code,char* dep_name)
- {
- int len;
- len=strlen(dep_code);
- code=new char[len+1];
- strncpy(code,dep_code,len); code[len]='\0';
- len=strlen(dep_name);
- name=new char[len+1];
- strncpy(name,dep_name,len); name[len]='\0';
- //cout<<"D\n";
- return;
- }
- DEPART(DEPART& obj)
- {
- int len;
- len=strlen(obj.code);
- code=new char[len+1];
- strncpy(code,obj.code,len); code[len]='\0';
- len=strlen(obj.name);
- name=new char[len+1];
- strncpy(name,obj.name,len); name[len]='\0';
- //cout<<"&D\n";
- return;
- }
- DEPART operator =(DEPART& obj)
- {
- int len;
- len=strlen(obj.code);
- code=new char[len+1];
- strncpy(code,obj.code,len); code[len]='\0';
- len=strlen(obj.name);
- name=new char[len+1];
- strncpy(name,obj.name,len); name[len]='\0';
- //cout<<"=D\n";
- return;
- }
- ~DEPART()
- {
- delete[] code;
- delete[] name;
- //cout<<"~D\n";
- return;
- }
- void print()
- {
- cout<<"Department code is "<<code<<",name is "<<name<<".\n";
- return;
- }
- private:
- char* code;
- char* name;
- };
- class EMPLOYEE{
- public:
- EMPLOYEE()
- {
- code=new char[CODE_LEN+1];
- strcpy(code,"");
- name=new char[NAME_LEN+1];
- strcpy(name,"");
- //cout<<"E_\n";
- return;
- }
- EMPLOYEE(char* wrk_code,char* wrk_name)
- {
- int len;
- len=strlen(wrk_code);
- code=new char[len+1];
- strncpy(code,wrk_code,len); code [len]='\0';
- len=strlen(wrk_name);
- name=new char[len+1];
- strncpy(name,wrk_name,len); name [len]='\0';
- //cout<<"E\n";
- return;
- }
- ~EMPLOYEE()
- {
- delete code;
- delete name;
- //cout<<"~E\n";
- return;
- }
- void set_depart(DEPART depart)
- {
- EMPLOYEE::depart=depart;
- return;
- }
- DEPART get_depart()
- {
- return depart;
- }
- void print()
- {
- cout<<"Employee code is "<<code<<",name is "<<name<<".He belongs to:\n";
- depart.print();
- return;
- }
- private:
- char* code;
- char* name;
- DEPART depart;
- };
- int main()
- {
- DEPART dep("01","Shipment Department");
- EMPLOYEE emp("LB","Liu Bing");
- emp.set_depart(dep);
- emp.print();
- dep=emp.get_depart();
- dep.print();
- return 0;
- }
复制代码 提示错误是:
8-7.cpp: In member function `DEPART DEPART:perator=(DEPART&)':
8-7.cpp:55: return-statement with no value, in function declared with a
non-void return type
8-7.cpp: In function `int main()':
8-7.cpp:133: no match for `DEPART& = DEPART' operator
8-7.cpp:46: candidates are: DEPART DEPART:perator=(DEPART&) |
|