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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题