代码如下,在我的vc++6.0中出现编译错误
#include
#include
using namespace std;
class String
{
public:
String(char const * str = NULL):m_str( strcpy(new char[strlen(str ? str:" ")+1],str ? str:" ")){}
//支持深拷贝构造的的拷贝构造函数
String(String const& that):m_str(strcpy(new char[strlen(that.m_str)+1],that.m_str)){}
String& operator= (String const& rhs)
{
if(&rhs != this)
{
String str = rhs;
swap(m_str,str.m_str);
}
return *this;
}
~String(void)
{
if(m_str)
{
delete[] m_str;
m_str = NULL;
}
}
char const* c_str(void) const
{
return m_str;
}
private:
char* m_str;
};
class A
{
public:
A(int data=0):m_data(data){}
private:
A(A const& that);
A& operator= (A const& rhs);
int m_data;
};
void print(ostream& os)
{
os<<"hello,world!"<<endl;
}
int main(void)
{
String s1="Hello,World!";
cout<< s1.c_str() <<endl;
String s2="你好,世界!";
s2 = s3;
cout<< s2.c_str()<<endl;
A a1(100);
A a3(200);
print(cout);
return 0;
}
这一段
String& operator= (String const& rhs)
{
if(&rhs != this)
{
String str = rhs;
swap(m_str,str.m_str);
}
return *this;
};
后面要加分号
() 和()的区别~~~~写代码尽量不要用中文,还有不建议使用VC6.0了,现在VS2015都出来了,个方面的比较智能,当然安装比较大,但vs2008就已经够了~
首先,环境变量设置正确了吗
C2018:是因为你拷贝的时候字符串的问题
C2146:丢失了分号
String& operator= (String const& rhs) //是中文()
s2 = s3; //s3未定义