程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 運行出錯-C++程序,出現以下錯誤是咋回事?

運行出錯-C++程序,出現以下錯誤是咋回事?

編輯:編程綜合問答
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;
}

最佳回答:


我的浏覽器代碼有時看不全,但是if(i=(int)(strlen(str)))應該是你寫錯了。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved