我在这里有代码,我试图理解运算符重载,我遇到了一个错误的路障,我完全不明白。该错误上有3个LNK2019错误,读取未解析的外部符号。我想知道错误是什么以及如何解决它。感谢您的帮助。
这是我的实施代码。
ComplexNumber::ComplexNumber()
{
double complexNumberValue = 0.00;
double realNumberValue = 0.00;
}
ComplexNumber::ComplexNumber(double real, double imaginary)
{
realNumberValue = real;
complexNumberValue = imaginary;
}
ComplexNumber ComplexNumber::operator*(const ComplexNumber& rho) const
{
double newValue = realNumberValue * rho.getValue();
ComplexNumber newNumber(newValue, complexNumberValue);
return newNumber;
}
ComplexNumber ComplexNumber::operator+(const ComplexNumber& rho) const
{
double newValue = realNumberValue + rho.getValue();
ComplexNumber newNumber(newValue, complexNumberValue);
return newNumber;
}
ComplexNumber ComplexNumber::operator-(const ComplexNumber& rho) const
{
double newValue = realNumberValue - rho.getValue();
ComplexNumber newNumber(newValue, complexNumberValue);
return newNumber;
}
ComplexNumber ComplexNumber::operator/(const ComplexNumber& rho) const
{
double newValue = realNumberValue / rho.getValue();
ComplexNumber newNumber(newValue, complexNumberValue);
return newNumber;
}
ComplexNumber ComplexNumber::operator=(const ComplexNumber& rho)
{
realNumberValue = rho.realNumberValue;
return *this;
}
bool operator==(const ComplexNumber& leftOp, const ComplexNumber& rightOp)
{
if (leftOp.getValue() == rightOp.getValue())
{
return true;
}
else
{
return false;
}
}
ostream& operator<<(ostream& out, const ComplexNumber& rho)
{
out << rho.getValue();
return out;
}
这是我的驱动程序代码。
#include "ComplexNumbers.h"
int main()
{
// Create complex numbers to do arithmentic with
ComplexNumber cm1(1, 2);
ComplexNumber cm2(1, -2);
// test addition operator
ComplexNumber cm3 = cm1 + cm2;
cout << cm3 << endl;
// test subtraction operator
ComplexNumber cm4 = cm1 - cm2;
cout << cm4 << endl;
// test multiplication operator
ComplexNumber cm5 = cm1 * cm2;
cout << cm5 << endl;
// test division operator
ComplexNumber cm6 = cm1 / cm2;
cout << cm6 << endl;
// test assignment operator
cm6 = cm5;
cout << cm6 << endl;
// test comparison operator
if (cm1 == cm2)
cout << "\nThey are equal.\n";
else
cout << "\nThey are not equal.";
ComplexNumber cm8(1, 2);
if (cm1 == cm8)
cout << "\nThey are equal.\n";
else
cout << "\nThey are not equal.";
system("PAUSE");
return 0;
}
这是请求的头文件
#include <string>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <stdexcept>
using namespace std;
class ComplexNumber
{
public:
//Constructor
//Purpose: Set the value of ComplexNumber to 0
//Parameters: None
//Returns: None
ComplexNumber();
//Parameterized Constructor
//Purpose: Set the value of ComplexNumber to the parameter
//Parameters: A double, and a double
//Returns: None
ComplexNumber(double, double);
//getValue
//Purpose:Getter for complexNumberValue
//Parameters: None
//Returns: the double value of object
double getValue() const;
//plusOverload
//Purpose: Overload the + operator
//Parameters: The ComplexNumber on the right side of the operator
//Returns: A new ComplexNumber object containing the sum
ComplexNumber operator+(const ComplexNumber&) const;
//minusOverload
//Purpose: Overload the - operator
//Parameters: The ComplexNumber on the right side of the operator
//Returns: A new ComplexNumber object containing the difference
ComplexNumber operator-(const ComplexNumber&) const;
//multiplicationOverload
//Purpose: Overload the * operator
//Parameters: The ComplexNumber on the right side of the operator
//Returns: A new ComplexNumber object containing the product
ComplexNumber operator*(const ComplexNumber&) const;
//divisionOverload
//Purpose: Overload the / operator
//Parameters: The ComplexNumber on the right side of the operator
//Returns: A new ComplexNumber object containing the quotient
ComplexNumber operator/(const ComplexNumber&) const;
//assignment
//Purpose: Overload the = operator
//Parameters: The two ComplexNumbers we want to assign
//Returns:
ComplexNumber operator=(const ComplexNumber&);
//comparison
//Purpose: Overload the == operator
//Parameters: The two ComplexNumbers we want to compare
//Returns:
bool operator==(const ComplexNumber&) const;
private:
double complexNumberValue;
double realNumberValue;
};
//Independant Functions
//streamInsertionOverload
//Purpose: Overload the stream insertion operator
//Parameters: The stream object to read from, and object to read to
//Returns:The stream
ostream& operator<<(const ostream&, const ComplexNumber&);
这是我的错误
错误1错误LNK2019:未解析的外部符号“public:bool __thiscall ComplexNumber :: operator ==(class ComplexNumber const&amp;)const”(?? 8ComplexNumber @@ QBE_NABV0 @@ Z)在函数_main中引用
错误2错误LNK2019:未解析的外部符号“class std :: basic_ostream&gt;&amp; __cdecl运算符&lt;&lt;(类std :: basic_ostream&gt; const&amp;,类ComplexNumber const&amp;)”(?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ ABV01 @ ABVComplexNumber @@@ Z)在函数_main中引用
错误3错误LNK2019:未解析的外部符号“public:double __thiscall ComplexNumber :: getValue(void)const”(?getValue @ ComplexNumber @@ QBENXZ)在函数“class std :: basic_ostream&gt;&amp; __cdecl operator&lt; &lt;(class std :: basic_ostream&gt;&amp;,class ComplexNumber const&amp;)“(?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ ABVComplexNumber @@@ Z)
错误4错误LNK1120:3个未解析的外部
答案 0 :(得分:1)
我已经意识到在我的头文件中,我有一个定义和声明的冲突。通过删除头文件中的const,我能够获得编译的程序。我还需要实现getValue()函数。