2 wddddddsd wddddddsd 于 2013.12.01 20:32 提问

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个回答

u012813250
u012813250   2013.12.03 00:26
已采纳

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!