当我尝试在我的.h文件中重载“ <
multiple definition of `operator<<(std::ostream&, complex_number&)'
但是当我在.cpp文件中移动运算符重载时,一切正常。我真的不知道发生了什么。有帮助吗?
这是我的初始代码:
(main.cpp非常简单,不包含任何重要内容)
complex_number.h
#ifndef COMPLEX_NUMBER_H
#define COMPLEX_NUMBER_H
#include <iostream>
using namespace std;
class complex_number
{
public:
complex_number();
complex_number(double, double);
virtual ~complex_number();
double Geta() const { return a; }
void Seta(double val) { a = val; }
double Getb() const { return b; }
void Setb(double val) { b = val; }
void print();
friend ostream & operator << (ostream &out, complex_number &cmp);
protected:
private:
double a;
double b;
};
ostream & operator << (ostream &out, complex_number &cmp) {
double a = cmp.Geta();
double b = cmp.Getb();
if (a == 0 && b == 0){
out << "0";
}
else if (a == 0) {
//if (b < 0) cout << "-";
if (b == -1) {
out << "-i";
}
if (b!=1) cout << b;
out << "i";
}
else if (b == 0) {
out << a;
} else {
out << a;
out << (b > 0 ? "+" : "-");
if (b!=1 && b!=-1) out << (b > 0 ? b : -1*b);
out << "i";
}
return out;
}
#endif // COMPLEX_NUMBER_H
complex_number.cpp
#include "complex_number.h"
#include <iostream>
#include <string>
using namespace std;
complex_number::complex_number()
{
//ctor
a = 0;
b = 0;
}
complex_number::complex_number(double a1, double b1)
{
//ctor
a = a1;
b = b1;
}
void complex_number::print() {
if (a == 0 && b == 0){
cout << "0";
return;
}
else if (a == 0) {
//if (b < 0) cout << "-";
if (b == -1) {
cout << "-i";
return;
}
if (b!=1) cout << b;
cout << "i";
return;
}
else if (b == 0) {
cout << a;
return;
}
cout << a;
cout << (b > 0 ? "+" : "-");
if (b!=1) cout << (b > 0 ? b : -1*b);
cout << "i";
return;
}
complex_number::~complex_number()
{
//dtor
}
因为您的.h文件包含在多个.cpp文件中,并且每个.cpp文件被分别编译为不同的编译单元,然后链接在一起,因此,<
如果您坚持将其保留在.h文件中,则有两种选择。
选项1:
内联<< <
inline ostream & operator << (ostream &out, complex_number &cmp) {
选项2:
使<
原因是:引用N3691-§11.3/ 4 [class.friend]
首先在friend声明中声明的函数具有外部链接(3.5)。否则,该函数将保留其先前的链接(7.1.1)。
complex_number.h
class complex_number;
static ostream & operator << (ostream &out, complex_number &cmp);
class complex_number
{
public:
complex_number();
complex_number(double, double);
virtual ~complex_number();
double Geta() const { return a; }
void Seta(double val) { a = val; }
double Getb() const { return b; }
void Setb(double val) { b = val; }
void print();
friend ostream & operator << (ostream &out, complex_number &cmp);
protected:
private:
double a;
double b;
};
static ostream & operator << (ostream &out, complex_number &cmp) {
double a = cmp.Geta();
....
}