报错信息如下:
错误 LNK2019 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Square &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVSquare@@@Z),函数 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Square (* const)[3])" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@QAY02VSquare@@@Z) 中引用了该符号 实验3 E:\vs2019\实验3\实验3\呜呜.obj 1
代码如下:
#include<iostream>
using namespace std;
class Square
{public:
Square()
{
int a[2][3];
for(int i=0;i<1;i++)
{
for (int j = 0; j < 2; j++) { a[i][j] = 0; }
}
};
friend Square operator +(Square& a, Square& b);
friend ostream & operator<< (ostream&, Square&);
friend istream & operator>> (istream&, Square&);
private:
int a[2][3];
};
Square operator+(Square& a, Square& b)
{
Square c;
int i, j;
for (i = 0; i < 1; i++)
{
for (j = 0; j < 2; j++)
{
c.a[i][j] = a.a[i][j] + b.a[i][j];
}
}
return c;
}
istream & operator>>(istream&input,Square a[2][3])
{
int i;
cout << "请输入矩阵第一行的数据:" << endl;
for(i=0;i<2;i++)
{
input >> a[0][i];
}
cout << "请输入矩阵第二行的数据:" << endl;
for(i=0;i<2;i++)
{
input >> a[1][i];
}
return input;
}
ostream & operator<<(ostream& output, Square a[2][3])
{
int i;
for (i = 0; i < 2; i++)
{
output << a[0][i];
}
cout << endl;
for (i = 0; i < 2; i++)
{
output << a[1][i];
}
return output;
}
int main()
{
Square a, b,c;
cout << "请输入矩阵a:" << endl;
cin >> a;
cout << "请输入矩阵b:" << endl;
cin >> b;
c = a + b;
cout << "结果为:" << c << endl;
return 0;
}
重载+时若无最后return 从,则不会报错LNK2019与LNK1120,只会报错让那边返回一个值
不知道你这个问题是否已经解决, 如果还没有解决的话: