求助啊,这段代买在VC++上可以运行,但是DEV c++就不行了
#include <iostream>using namespace std;
class sample
{
char*string;
public:
sample(){string=new char('\0');}
sample(const sample &ob);
~sample(){if(string) delete string;cout<<"Freeing string\n";}
void show_string(){cout<<string<<"\n";}
void set_string(char*s);
sample operator=(sample &ob);
};
void sample::set_string(char*s)
{
string=new char[strlen(s)+1];
if(!string)
{
cout<<"Allocation error\n";
exit(1);
};
strcpy(string,s);
}
sample::sample(const sample &ob)
{
string=new char[strlen(ob.string)+1];
if(!string)
{
cout<<"Allocation error\n";
exit(1);
};
strcpy(string,ob.string);
}
sample sample::operator =(sample &ob)
{
if(strlen(ob.string)>strlen(string))
{
delete string;
string=new char[strlen(ob.string)+1];
if(!string)
{
cout<<"Allocation error\n";
exit(1);
}
}
strcpy(string,ob.string);
return *this;
}
sample input_string()
{
char instr[80];
sample str;
cout<<"Enter a string:";
cin>>instr;
str.set_string(instr);
return str;
}
int main()
{
sample ob;
ob=input_string(); //DEV C++中显示这里是错的:no match for 'operator=' in 'ob = input_string()()' ,而VC++中没问题。
ob.show_string();
return 0;
}
求大神帮忙改下,能在DEVc++中也运行。