LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 844|回复: 13

这个程序错在哪里?

[复制链接]
发表于 2003-6-15 17:58:23 | 显示全部楼层 |阅读模式
要求只修改类DEPART,其它不能修改,兄弟们帮帮手
  1. #include <iostream.h>
  2. #include <string.h>
  3. const int CODE_LEN=8;
  4. const int NAME_LEN=40;
  5. class DEPART{
  6. public:
  7.    DEPART()
  8.    {
  9.      code=new char[CODE_LEN+1];
  10.      strcpy(code,"");
  11.      name=new char[NAME_LEN+1];
  12.      strcpy(name,"");
  13.      cout<<"D_\n";
  14.      return;
  15.    }
  16.    DEPART(char* dep_code,char* dep_name)
  17.    {
  18.      int len;
  19.      len=strlen(dep_code);
  20.      code=new char[len+1];
  21.      strncpy(code,dep_code,len);  code[len]='\0';
  22.      len=strlen(dep_name);
  23.      name=new char[len+1];
  24.      strncpy(name,dep_name,len);  name[len]='\0';
  25.      cout<<"D\n";
  26.      return;
  27.      }
  28.      ~DEPART()
  29.    {
  30.      delete code;
  31.      delete name;
  32.      cout<<"~D\n";
  33.      return;
  34.    }
  35.    void print()
  36.    {
  37.      cout<<"Department code is "<<code<<",name is "<<name<<".\n";
  38.      return;
  39.    }
  40. private:
  41.    char* code;
  42.    char* name;
  43. };
  44. class EMPLOYEE{
  45. public:
  46.    EMPLOYEE()
  47.    {
  48.      code=new char[CODE_LEN+1];
  49.      strcpy(code,"");
  50.      name=new char[NAME_LEN+1];
  51.      strcpy(name,"");
  52.      cout<<"E_\n";
  53.      return;
  54.    }
  55.    EMPLOYEE(char* wrk_code,char* wrk_name)
  56.    {
  57.      int len;
  58.      len=strlen(wrk_code);
  59.      code=new char[len+1];
  60.      strncpy(code,wrk_code,len);  code [len]='\0';
  61.      len=strlen(wrk_name);
  62.      name=new char[len+1];
  63.      strncpy(name,wrk_name,len);  name [len]='\0';
  64.      cout<<"E\n";
  65.      return;
  66.    }
  67.    ~EMPLOYEE()
  68.    {
  69.      delete code;
  70.      delete name;
  71.      cout<<"~E\n";
  72.      return;
  73.    }
  74.    void set_depart(DEPART depart)
  75.    {
  76.      EMPLOYEE::depart=depart;
  77.      return;
  78.    }
  79.    DEPART get_depart()
  80.    {
  81.      return depart;
  82.    }
  83.    void print()
  84.    {
  85.      cout<<"Employee code is "<<code<<",name is "<<name<<".He belongs to:\n";
  86.      depart.print();
  87.      return;
  88.    }
  89. private:
  90.    char* code;
  91.    char* name;
  92.    DEPART depart;
  93. };
  94. int main()
  95. {
  96.    DEPART dep("01","Shipment Department");
  97.    EMPLOYEE emp("LB","Liu Bing");
  98.    emp.set_depart(dep);
  99.    emp.print();
  100.    dep=emp.get_depart();
  101.    dep.print();
  102.    return 0;
  103. }
复制代码
发表于 2003-6-15 18:29:01 | 显示全部楼层
应该给DEPART类提供复制构造函数和赋值运算符,加上这两个成员就好了
发表于 2003-6-15 20:21:29 | 显示全部楼层
下次应该注意代码的缩进。
发表于 2003-6-15 20:36:35 | 显示全部楼层
楼主也应该看看vb码说明(斑竹签名)。
代码没有宿进,
你的类。没有use std::cout等
delete用delete []?
private 放在前面好些
 楼主| 发表于 2003-6-15 22:58:23 | 显示全部楼层
对不起,我已把缩进改正过来了.
以下是我在~DEPART()前面加进的的代码,但编译不了.
  1.   DEPART(DEPART& obj)
  2.   {
  3.     int len;
  4.     len=strlen(obj.code);
  5.     code=new char[len+1];
  6.     strncpy(code,obj.code,len); code[len]='\0';
  7.     len=strlen(obj.name);
  8.     name=new char[len+1];
  9.     strncpy(name,obj.name,len); name[len]='\0';
  10.     //cout<<"&D\n";
  11.     return;
  12.   }
  13.   DEPART operator =(DEPART& obj)
  14.   {
  15.     int len;
  16.     len=strlen(obj.code);
  17.     code=new char[len+1];
  18.     strncpy(code,obj.code,len); code[len]='\0';
  19.     len=strlen(obj.name);
  20.     name=new char[len+1];
  21.     strncpy(name,obj.name,len); name[len]='\0';
  22.     //cout<<"=D\n";
  23.     return;
  24.     }
复制代码
发表于 2003-6-15 23:07:40 | 显示全部楼层
怎么编译不了,什么错误?
把:
#include <iostream.h>
#include <string.h>
改成:
#include <iostream>
#include <string>
using namespace std;
试试。
发表于 2003-6-15 23:12:35 | 显示全部楼层
还有,麥頭说的,应该用delete[]。
类里只要涉及到成员分配内存,就应该提供构造函数、复制构造函数、赋值运算符、析构函数,你的EMPLOYEE类也不完全。
 楼主| 发表于 2003-6-15 23:47:13 | 显示全部楼层
因为题目是约定了只能修改DEPART类的.
我把程序改成了这样,还是通过不了
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. const int CODE_LEN=8;
  5. const int NAME_LEN=40;
  6. class DEPART{
  7. public:
  8.   DEPART()
  9.   {
  10.     code=new char[CODE_LEN+1];
  11.     strcpy(code,"");
  12.     name=new char[NAME_LEN+1];
  13.     strcpy(name,"");
  14.     //cout<<"D_\n";
  15.     return;
  16.   }
  17.   DEPART(char* dep_code,char* dep_name)
  18.   {
  19.     int len;
  20.     len=strlen(dep_code);
  21.     code=new char[len+1];
  22.     strncpy(code,dep_code,len);  code[len]='\0';
  23.     len=strlen(dep_name);
  24.     name=new char[len+1];
  25.     strncpy(name,dep_name,len);  name[len]='\0';
  26.     //cout<<"D\n";
  27.     return;
  28.     }
  29.   DEPART(DEPART& obj)
  30.   {
  31.     int len;
  32.     len=strlen(obj.code);
  33.     code=new char[len+1];
  34.     strncpy(code,obj.code,len); code[len]='\0';
  35.     len=strlen(obj.name);
  36.     name=new char[len+1];
  37.     strncpy(name,obj.name,len); name[len]='\0';
  38.     //cout<<"&D\n";
  39.     return;
  40.   }
  41.   DEPART operator =(DEPART& obj)
  42.   {
  43.     int len;
  44.     len=strlen(obj.code);
  45.     code=new char[len+1];
  46.     strncpy(code,obj.code,len); code[len]='\0';
  47.     len=strlen(obj.name);
  48.     name=new char[len+1];
  49.     strncpy(name,obj.name,len); name[len]='\0';
  50.     //cout<<"=D\n";
  51.     return;
  52.     }
  53.   ~DEPART()
  54.   {
  55.     delete[] code;
  56.     delete[] name;
  57.     //cout<<"~D\n";
  58.     return;
  59.   }
  60.   void print()
  61.   {
  62.     cout<<"Department code is "<<code<<",name is "<<name<<".\n";
  63.     return;
  64.   }
  65. private:
  66.   char* code;
  67.   char* name;
  68. };
  69. class EMPLOYEE{
  70. public:
  71.   EMPLOYEE()
  72.   {
  73.     code=new char[CODE_LEN+1];
  74.     strcpy(code,"");
  75.     name=new char[NAME_LEN+1];
  76.     strcpy(name,"");
  77.     //cout<<"E_\n";
  78.     return;
  79.   }
  80.   EMPLOYEE(char* wrk_code,char* wrk_name)
  81.   {
  82.     int len;
  83.     len=strlen(wrk_code);
  84.     code=new char[len+1];
  85.     strncpy(code,wrk_code,len);  code [len]='\0';
  86.     len=strlen(wrk_name);
  87.     name=new char[len+1];
  88.     strncpy(name,wrk_name,len);  name [len]='\0';
  89.     //cout<<"E\n";
  90.     return;
  91.   }
  92.   ~EMPLOYEE()
  93.   {
  94.     delete code;
  95.     delete name;
  96.     //cout<<"~E\n";
  97.     return;
  98.   }
  99.   void set_depart(DEPART depart)
  100.   {
  101.     EMPLOYEE::depart=depart;
  102.     return;
  103.   }
  104.   DEPART get_depart()
  105.   {
  106.     return depart;
  107.   }
  108.   void print()
  109.   {
  110.     cout<<"Employee code is "<<code<<",name is "<<name<<".He belongs to:\n";
  111.     depart.print();
  112.     return;
  113.   }
  114. private:
  115.   char* code;
  116.   char* name;
  117.   DEPART depart;
  118. };
  119. int main()
  120. {
  121.   DEPART dep("01","Shipment Department");
  122.   EMPLOYEE emp("LB","Liu Bing");
  123.   emp.set_depart(dep);
  124.   emp.print();
  125.   dep=emp.get_depart();
  126.   dep.print();
  127.   return 0;
  128. }
复制代码
提示错误是:
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&)
发表于 2003-6-16 00:19:01 | 显示全部楼层
[修改如下:
g++
注意构造函数没有return;
operator (const depart&)


  1. #include <iostream>
  2. #include <string>

  3. using namespace std;

  4. const int CODE_LEN=8;
  5. const int NAME_LEN=40;

  6. class DEPART{
  7. private:
  8.   char* code;
  9.   char* name;
  10. public:
  11.   DEPART()
  12.   {
  13.     code=new char[CODE_LEN+1];
  14.     strcpy(code,"");
  15.     name=new char[NAME_LEN+1];
  16.     strcpy(name,"");
  17.     //cout<<"D_\n";
  18.   }
  19.   DEPART(char* dep_code,char* dep_name)
  20.   {
  21.     int len;

  22.     len=strlen(dep_code);
  23.     code=new char[len+1];
  24.     strncpy(code,dep_code,len);  code[len]='\0';
  25.     len=strlen(dep_name);
  26.     name=new char[len+1];
  27.     strncpy(name,dep_name,len);  name[len]='\0';
  28.     //cout<<"D\n";
  29.     }
  30.   DEPART(const DEPART& obj)
  31.   {
  32.     int len;
  33.     len=strlen(obj.code);
  34.     code=new char[len+1];
  35.     strncpy(code,obj.code,len);
  36.     code[len]='\0';
  37.     len=strlen(obj.name);
  38.     name=new char[len+1];
  39.     strncpy(name,obj.name,len);
  40.     name[len]='\0';
  41.     //cout<<"&D\n";
  42.    }
  43.   DEPART &operator=(const DEPART& obj)
  44.   {
  45.     int len;
  46.     len=strlen(obj.code);
  47.     code=new char[len+1];
  48.     strncpy(code,obj.code,len); code[len]='\0';
  49.     len=strlen(obj.name);
  50.     name=new char[len+1];
  51.     strncpy(name,obj.name,len); name[len]='\0';
  52.     //cout<<"=D\n";
  53.     return *this;
  54.     }
  55.   ~DEPART()
  56.   {
  57.     delete[] code;
  58.     delete[] name;
  59.     //cout<<"~D\n";
  60.   }
  61.   void print()
  62.   {
  63.     cout<<"Department code is "<<code<<",name is "<<name<<".\n";
  64.     return;
  65.   }
  66. };

  67. class EMPLOYEE{
  68. private:
  69.   char* code;
  70.   char* name;
  71.   DEPART depart;
  72. public:
  73.   EMPLOYEE()
  74.   {
  75.   
  76.     code=new char[CODE_LEN+1];
  77.     strcpy(code,"");
  78.     name=new char[NAME_LEN+1];
  79.     strcpy(name,"");
  80.     //cout<<"E_\n";
  81.   }
  82.   EMPLOYEE(char* wrk_code,char* wrk_name)
  83.   {
  84.     int len;

  85.     len=strlen(wrk_code);
  86.     code=new char[len+1];
  87.     strncpy(code,wrk_code,len);  code [len]='\0';
  88.     len=strlen(wrk_name);
  89.     name=new char[len+1];
  90.     strncpy(name,wrk_name,len);  name [len]='\0';
  91.     //cout<<"E\n";
  92.   }
  93.   ~EMPLOYEE()
  94.   {
  95.   
  96.     delete code;
  97.     delete name;
  98.     //cout<<"~E\n";
  99.   }
  100.   void set_depart(DEPART &depart)
  101.   {
  102.     EMPLOYEE::depart=depart;
  103.     return;
  104.   }
  105.   DEPART get_depart()
  106.   {
  107.     return depart;
  108.   }
  109.   void print()
  110.   {
  111.     cout<<"Employee code is "<<code<<",name is "<<name<<".He belongs to:\n";
  112.     depart.print();
  113.     return;
  114.   }

  115. };

  116. int main()
  117. {
  118.   DEPART dep("01","Shipment Department");
  119.   EMPLOYEE emp("LB","Liu Bing");

  120.   emp.set_depart(dep);
  121.   emp.print();
  122.   dep=(emp.get_depart());
  123.   dep.print();
  124.   return 0;
  125. }
复制代码


运行:
root@localhost c++_file]# a.out
Employee code is LB,name is Liu Bing.He belongs to:
Department code is 01,name is Shipment Department.
Department code is 01,name is Shipment Department.
[root@localhost c++_file]#
发表于 2003-6-16 01:23:33 | 显示全部楼层
麥頭的函数形式正确,不过代码还有一点问题,在复制构造函数和赋值运算符里首先应该判断传入的对象不是本对象,&obj != this,虽然给自己赋值不经常出现,但还是应该避免错误。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表