请C++高手修改

来源:百度知道 编辑:UC知道 时间:2024/05/26 12:23:47
#include<iostream.h>
#include<string.h>
class jia
{
public:
jia(char stl[5]);
jia operator+(jia );
void show();
private:
char str[5];
};
jia::jia(char stl[5])
{
strcpy(str,stl);
}
jia jia::operator+(jia)
{
jia A,B;
return strcat(A.str,B.str);

}
void jia::show()
{
cout<<str<<endl;
}
void main()
{
jia A="abcd";
jia B="efgh";
A.show();
B.show();
(A+B).show();
}

你应该给jia这个类提供一个默认构造函数,因为现在当程序进行到
jia jia::operator+(jia)
{
jia A,B;
return strcat(A.str,B.str);

}
时,你声明了2个jia类型的变量A,B,但由于该类没有默认构造函数,所以初始化失败,导致出错。
你可以把jia(char stl[5]); 这句简单的改为:
jia(char stl[5]="");
注意那两个紧挨着的"",作为默认参数,这样就编译连接通过了!快试试吧!