C++ 重载输入/输出运算符,为什么它以这种方式工作?
问题是:我得到一个关于这两个操作符的链接器错误。更具体地说,以下是确切的错误消息: 注:我有一个“有效的解决方案”,如果你读到底我提到它。我不知道为什么它是这样工作的,而不是这样。我更希望这个方法能够工作,因为它看起来更干净,没有在我的声明上面挂起输入/输出操作符。对不起,太长了。我删除了一些不必要的东西,比如我使用的其他重载操作符,因为它们不相关C++ 重载输入/输出运算符,为什么它以这种方式工作?,c++,operator-overloading,operator-keyword,C++,Operator Overloading,Operator Keyword,问题是:我得到一个关于这两个操作符的链接器错误。更具体地说,以下是确切的错误消息: 注:我有一个“有效的解决方案”,如果你读到底我提到它。我不知道为什么它是这样工作的,而不是这样。我更希望这个方法能够工作,因为它看起来更干净,没有在我的声明上面挂起输入/输出操作符。对不起,太长了。我删除了一些不必要的东西,比如我使用的其他重载操作符,因为它们不相关 poly.obj : error LNK2005: "class std::basic_istream<char,struct std::ch
poly.obj : error LNK2005: "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl operator>>(class std::basic_istream<char,struct std::char_traits<char> > &,class Poly &)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVPoly@@@Z) already defined in lab1.obj
1>poly.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Poly const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVPoly@@@Z) already defined in lab1.obj
1>d:\... fatal error LNK1169: one or more multiply defined symbols found
poly.obj:error LNK2005:“class std::basic\u istream&\u cdecl操作符>>(class std::basic\u istream&,class poly&)”(??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@AAV01@AAVPoly@@@Z) 已在lab1.obj中定义
1> poly.obj:错误LNK2005:“类std::basic_ostream&__cdecl运算符(从注释中移动)
如果您编写自由函数的主体(如运算符(从注释移动)
如果您编写自由函数体(如运算符)如果您编写自由函数体(如operator@MatteoItalia好的,这肯定解决了这个问题。介意解释一下ODR代表什么吗?如果你写一个自由函数的主体(就像你的operator@MatteoItalia这肯定解决了这个问题。介意解释一下ODR代表什么吗?
//file poly.h
#ifndef POLY_H
#define POLY_H
#include <iostream>
using namespace std;
class Poly {
public:
Poly(int = 0, int = 0); //default constructor
Poly(const Poly &); //copy constructor
~Poly(); //destructor
friend ostream& operator<<(ostream& output, const Poly& thePoly);
friend istream& operator>>(istream& input, Poly& thePoly);
private:
int* polynomial;
int maxExponent;
};
istream& operator>>(istream& input, Poly& thePoly) {
return input;
}
ostream& operator<<(ostream& output, const Poly& thePoly) {
bool isZero = true;
for (int i = thePoly.maxExponent; i > 0; i--) {
if (thePoly.polynomial[i] != 0) {
if (thePoly.polynomial[i] < 0) {
output << " -";
}
else {
output << " +";
}
output << thePoly.polynomial[i];
if (i != 0) {
output << "x";
}
if (i != 1) {
output << "^";
}
output << i;
isZero = false;
}
}
if (isZero) {
output << " 0";
}
return output;
}
#endif
// DO NOT change anything in this file. Your code must compile and give the
// correct output with this main on the linux machines.
// Make sure the file containing the member function source is: poly.cpp
// Use all lowercase in the file names.
// This main does not do a thorough job of testing. When testing arrays,
// be sure to test the middle and also all the boundary conditions. Test
// values on the boundary and outside the boundaries, i.e., too big/small.
#include "poly.h"
#include <iostream>
using namespace std;
int main() {
Poly A(5, 7), B(3, 4), C(2), D(A), X, Y;
Poly A2, B2, Z1, Z2;
// set polynomials A and B to desired values
// A = +5x^7 -4x^3 +10x -2
// B = +3x^4 +1x^3
cout << "Enter terms for polynomial A. Enter a coefficient " << endl
<< "then exponent for each term. Enter -1 -1 to terminate." << endl;
cin >> A; // or use a bunch of setCoeff
cout << "Enter terms for polynomial B. Enter a coefficient " << endl
<< "then exponent for each term. Enter -1 -1 to terminate." << endl;
cin >> B; // or use a bunch of setCoeff
// outputs exactly what is in quotes: "A = +5x^7 -4x^3 +10x -2"
cout << "A =" << A << endl;
// outputs exactly what is in quotes: "B = +3x^4 +1x^3"
cout << "B =" << B << endl << endl;
return 0;
}
#ifndef POLY_H
#define POLY_H
#include <iostream>
using namespace std;
class Poly {
friend istream& operator>>(istream& input, Poly& thePoly) {
return input;
}
friend ostream& operator<<(ostream& output, const Poly& thePoly) {
bool isZero = true;
for (int i = thePoly.maxExponent; i > 0; i--) {
if (thePoly.polynomial[i] != 0) {
if (thePoly.polynomial[i] < 0) {
output << " -";
}
else {
output << " +";
}
output << thePoly.polynomial[i];
if (i != 0) {
output << "x";
}
if (i != 1) {
output << "^";
}
output << i;
isZero = false;
}
}
if (isZero) {
output << " 0";
}
return output;
}
public:
Poly(int = 0, int = 0); //default constructor
Poly(const Poly &); //copy constructor
~Poly(); //destructor
private:
int* polynomial;
int maxExponent;
};
#endif