Bri*_*per 3 c++ class operator-overloading complex-numbers
调试我的程序时,我不断收到这两个错误:
main.obj:错误LNK2005:"class std :: basic_istream>&__cdecl operator >>(class std :: basic_istream>&,class Complex&)"(?? 5 @ YAAAV?$ basic_istream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ AAVComplex @@@ Z)已在Imaginary.obj中定义
1> main.obj:错误LNK2005:"class std :: basic_ostream>&__ cdecl operator <<(class std :: basic_ostream>&,class complex const&)"(?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ AAV01 @ ABVComplex @@@ Z)已在Imaginary.obj中定义
我已经尝试重新启动我的项目,以确保我创建了一个控制台应用程序,而不是偶然的事情,但这不起作用.任何方向将不胜感激.
这是我的代码:
imaginary.h
#ifndef imaginary_h_
#define imaginary_h_
#include <iostream>
class Complex
{
friend std::ostream & operator << (std::ostream& os, const Complex&);
friend std::istream & operator >> (std::istream& is, Complex&);
private:
double real;
double imag;
char op;
public:
double r = real;
double i = imag;
char o = op;
Complex()
{
}
Complex(double r, double i, char o)
{
}
bool userTest();
};
#endif
std::ostream & operator << (std::ostream & os, const Complex & complex)
{
os << complex.r << complex.o << complex.i << "i" << "/n" << "/n";
return os;
}
std::istream & operator >> (std::istream & is, Complex & complex)
{
is >> complex.r >> complex.o >> complex.i;
return is;
}
Run Code Online (Sandbox Code Playgroud)
Imaginary.cpp
#include "imaginary.h"
#include <iostream>
bool Complex::userTest()
{
if (op == '+' || op == '-')
{
return true;
}
else
{
std::cout << "Incorrect operand entry, please enter complex number as x+yi" << "/n" << "/n";
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "imaginary.h"
#include <iostream>
int main()
{
double userInput;
std::cout << "Here you will add or subtract complex numbers." << "/n" << "/n";
do
{
std::cout << "Please enter the real part of you complex number: " << "/n" << "/n";
Complex complex;
std::cin >> complex;
std::cout << "/n" << "/n" << "/n/t" << "Note: Remember to enter your complex number in the format of x+yi to avoid an error";
if (complex.userTest())
{
std::cout << complex << "/n" << "/n";
}
std::cout << "Would you like to perform another complex number operation? " << "/n" << "/n/t";
std::cout << "Choose 1 for Yes and 2 for No" << "/n" << "/n";
std::cin >> userInput;
} while (userInput == 1);
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是你已经定义了
std::ostream & operator<<(std::ostream &os, const Complex &complex) {
os << complex.r << complex.o << complex.i << "i" << "/n" << "/n";
return os;
}
std::istream & operator>>(std::istream &is, Complex &complex) {
is >> complex.r >> complex.o >> complex.i;
return is;
}
Run Code Online (Sandbox Code Playgroud)
在您的头文件中,因为它包含在其中imaginary.cpp并且main.cpp它们被多次定义。这违反了C++的单一定义规则。
你需要移动的实施operator <<,并operator >>在.cpp文件中,只留在头文件中的声明。这将意味着拥有
std::ostream & operator<<(std::ostream &os, const Complex &complex);
std::istream & operator>>(std::istream &is, Complex &complex);
Run Code Online (Sandbox Code Playgroud)
在类定义之后的头文件中
class Complex {
...
};
Run Code Online (Sandbox Code Playgroud)
而在结局之前#endif。然后在imaginary.cpp你会有定义
std::ostream & operator<<(std::ostream &os, const Complex &complex) {
os << complex.r << complex.o << complex.i << "i" << "/n" << "/n";
return os;
}
std::istream & operator>>(std::istream &is, Complex &complex) {
is >> complex.r >> complex.o >> complex.i;
return is;
}
Run Code Online (Sandbox Code Playgroud)
您有两种选择:
inline.例如,在头文件imaginary.hpp中:
inline // <-- Add this line
std::ostream & operator << (std::ostream & os, const Complex & complex)
{
os << complex.r << complex.o << complex.i << "i" << "/n" << "/n";
return os;
}
inline // <-- Add this line
std::istream & operator >> (std::istream & is, Complex & complex)
{
is >> complex.r >> complex.o >> complex.i;
return is;
}
#endif // imaginary_h_ <-- This line was moved.
Run Code Online (Sandbox Code Playgroud)
或者您可以简单地将实现移动到您的imaginary.cpp文件中.
| 归档时间: |
|
| 查看次数: |
4061 次 |
| 最近记录: |