该上机实验环境 linux mint IDE:qt5.11 代码复制到windows下vs2017报错,提示char* 类型不能直接赋值字符串
在linux mint下可以运行,测试目的:检验复制构造函数以及左移运算符在输出类对象方面的作用
#include <iostream> #include<string.h> using namespace std; class mycoach { public: friend ostream & operator<<(ostream& out,mycoach&t); mycoach() { age=22; c_name=new char[1]; strcpy(c_name,""); } mycoach(char * name,int age) { this->age=age; c_name=new char[strlen(name)+1];//never forgot allowa space strcpy(c_name,name); } mycoach(const mycoach &t) { this->age=t.age; strcpy(this->c_name,t.c_name); } ~mycoach() { if(c_name!=NULL) { delete [] c_name; c_name=NULL; } } mycoach& operator=(const mycoach& t) { if (c_name!=NULL) { delete[] c_name; age=22; c_name=NULL; } c_name=new char[strlen(t.c_name)+1]; strcpy(c_name,t.c_name); age=t.age; return *this;//this is a pointer *this is value } void print() { cout<<"hello~emma "<<this->c_name<<" emma "<<this->age<<" years old"; } private: char*name[32]; char * c_name; int age; }; ostream & operator<<(ostream& out,mycoach&t) { out<<"大家好~我是:"<<t.c_name<<",今年"<<t.age<<endl; } int main() { // mycoach cpc("陈培昌",22); mycoach fgf; fgf=cpc; cout<<fgf<<endl; mycoach fgf2("付高峰",30); fgf=fgf2; cout<<fgf<<endl; cout << "Hello World!" << endl; return 0; }