Thiswhyme 2013-12-01 12:32 采纳率: 100%
浏览 2228
已采纳

C++程序,出现以下错误是咋回事?

//StringClass.hCSDN移动问答

#include
#include
using namespace std;

class String
{
char* str;
public:
String() {str=NULL;}
String(const char* p)
{
str=new char[strlen(p)+1];
strcpy(str, p);
}
String(const String& s)
{
str=new char[strlen(s.str)+1];
strcpy(str, s.str);
}
~String()
{
delete []str;
str=NULL;
}

//取字符串长度
int length() const {return strlen(str); }

//取字符串第i位的引用
char& operator
{
if(i=(int)(strlen(str)))
{
cerr<<"超出字符串范围!\n";
exit(-1);
}
return str[i];
}

//用于常量对象
char operator const
{
if(i<0 || (int)(strlen(str)))
{
cerr<<"超出字符串范围!\n";
exit(-1);
}
return str[i];
}

//返回字符串指针
operator char* () const {return str; }

//拷贝字符串
String& operator=(const char *p)
{
char *p1=new char[strlen(p)+1]; //申请资源
strcpy(p1, p);
delete []str;
str=p1;
cout<<"Const char"<<endl;
return *this;
}

String& operator=(const String& s)
{
if(this==&s) return *this;
*this=s.str;
cout<<"String="<<endl;
return *this;
}

//拼接字符串
String& operator+=(char* p)
{
char* p1=new char[strlen(str)+strlen(p)+1];
strcpy(p1, str);
strcat(p1, p);
delete []str;
str=p1;
return *this;
}

String& operator+=(const String& s)
{
*this+=s.str;
return *this;
}

//比较两个字符串
friend bool operator==(const String &s1, const String &s2);
friend bool operator==(const String &s, const char* p);
friend bool operator==(const char *p, const String &s);
friend ostream& operator<<(ostream& os, const String &s);
};

bool operator==(const String& s1, const String& s2)
{
return (strcmp(s1.str, s2.str)==0);
}

bool operator==(const String& s, const char* p)
{
return (strcmp(s.str, p)==0);
}

bool operator==(const char* p, const String& s)
{
return (strcmp(p, s.str)==0);
}

ostream& operator<<(ostream& os, const String& s)
{
os<<s.str<<endl;
return os;
}

String operator+(const String& s1, const String& s2)
{
String temp(s1);
temp+=s2;
return temp;
}

String operator+(const String& s, const char* p)
{
String temp(s);
temp+=p;
return temp;
}

String operator+(const char* p, const String &s)
{
String temp(p);
temp+=s;
return temp;
}

//Employer.cpp

#include "StringClass.h"
#include
#include
using namespace std;

class Employee //普通职员类
{
String name; //String为例7.7中定义的字符串类
int salary;
public:
Employee(const char *s, int n=0):name(s){ salary=n; }
void set_salary(int n) { salary=n; }
int get_salary() const {return salary; }
String get_name() const {return name;}
friend ostream& operator<<(ostream& os, const Employee& e);
};

ostream& operator<<(ostream& os, const Employee& e)
{
os<<"Name is:"<<e.name<<"Salary is:"<<e.salary<<endl;
return os;
}
const int MAX_NUM_OF_EMPS=20;

class Manager: public Employee //部门经理类
{
Employee *group[MAX_NUM_OF_EMPS];
int num_of_emps;
public:
Manager(const char *s, int n=0): Employee(s,n) {num_of_emps=0;}
Employee *add_employee(Employee *e)
{
if(num_of_emps>=MAX_NUM_OF_EMPS) return NULL;
group[num_of_emps]=e;
num_of_emps++;
return e;
}
Employee *remove_employee(Employee *e)
{
int i;
for(i=0; i if(e->get_name()==group[i]->get_name())
break;
if(i<num_of_emps)
{
for(int j=i+1; j<num_of_emps; j++)
group[j-1]=group[j];
num_of_emps--;
return e;
}
else
return NULL;
}

const Employee *SearchEmployeeOfHighSalary()
{
// int highsalary=group[0]->get_salary();
int pos=0;
for(int i=1; i if(group[pos]->get_salary()get_salary())
{
// highsalary=group[i]->get_salary();
pos=i;
}
return group[pos];
}

~Manager()
{
for(int i=0; i<num_of_emps; i++)
if(group[i]!=NULL)
delete group[i];

// delete []group;

}

friend ostream& operator<<(ostream& os, const Manager& m);
};

ostream& operator<<(ostream& os, const Manager& m)
{
// os<<m.name<<m.salary

os<<(Employee)m;
int i;
for(i=0; i<m.num_of_emps; i++)
os<<*(m.group[i]);
return os;
}

int main()
{
Employee e1("Jack", 1000), e2("Jane", 2000), e3("John", 3000);
Manager m("Mark",4000);
m.add_employee(&e1);
m.add_employee(&e2);
m.add_employee(&e3);

cout<<"List Employee Before"<<endl;
cout<<m<<endl;

cout<<endl;
cout<<"Employee of High Salary is:"<<endl;
cout<<*(m.SearchEmployeeOfHighSalary());

m.remove_employee(&e2);

cout<<endl;
cout<<"List Employee After"<<endl;
cout<<m<<endl;
return 0;
}

  • 写回答

1条回答 默认 最新

  • JOHN_STOCKTON_ 2013-12-02 16:26
    关注

    我的浏览器代码有时看不全,但是if(i=(int)(strlen(str)))应该是你写错了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写
  • ¥15 graphpad prism 三因素重复测定报错