C++重载<<时出现 error LNK2019: 无法解析的外部符号 要实现的是输出复数 30
C++重载<<时出现errorLNK2019:无法解析的外部符号要实现的是输出复数//在main.cpp中#include<iostream>#include"E1.h"...
C++重载<<时出现 error LNK2019: 无法解析的外部符号
要实现的是输出复数
//在main.cpp中
#include<iostream>
#include"E1.h"
using namespace std;
using namespace MATH;
void main(){
complexe en(3,4);
complexe em(3,4);
complexe am;
am=en*em;
cout<<am; //只要加入这句话就出现错误,如果把它删了是没有错的
}
//在E1.h中
#include<iostream>
namespace MATH{
class complexe{
private:
double p_re;
double p_im;
public:
complexe(double re=0, double im=0):p_re(re),p_im(im){}
double Re(){return p_re;}
double Im(){return p_im;}
double module();
complexe conjugue(){
return complexe(p_re,-p_im);
}
};
const complexe operator+(complexe &p1,complexe &p2);
const complexe operator*(complexe &p1,complexe &p2);
std::ostream& operator<<(std::ostream& C,MATH::complexe& pe);
}
//E1.cpp中
#include<cmath>
#include"E1.h"
double MATH::complexe::module(){
double m,p;
p=p_im*p_im+p_re*p_re;
m=sqrt(p);
return m;
}
const MATH::complexe MATH::operator+(MATH::complexe &p1, MATH::complexe &p2){
return MATH::complexe(p1.Re() +p2.Re(),p1.Im() +p2.Im() );
}
const MATH::complexe MATH::operator*(MATH::complexe &p1, MATH::complexe &p2){
return MATH::complexe(p1.Re()*p2.Re()-p1.Im()*p2.Im(),p1.Re()*p2.Im()+p1.Im()*p2.Re());
}
std::ostream& operator<<(std::ostream& C,MATH::complexe& pe){
C<<pe.Re()<<"+"<<pe.Im()<<"i";
return C;
} 展开
要实现的是输出复数
//在main.cpp中
#include<iostream>
#include"E1.h"
using namespace std;
using namespace MATH;
void main(){
complexe en(3,4);
complexe em(3,4);
complexe am;
am=en*em;
cout<<am; //只要加入这句话就出现错误,如果把它删了是没有错的
}
//在E1.h中
#include<iostream>
namespace MATH{
class complexe{
private:
double p_re;
double p_im;
public:
complexe(double re=0, double im=0):p_re(re),p_im(im){}
double Re(){return p_re;}
double Im(){return p_im;}
double module();
complexe conjugue(){
return complexe(p_re,-p_im);
}
};
const complexe operator+(complexe &p1,complexe &p2);
const complexe operator*(complexe &p1,complexe &p2);
std::ostream& operator<<(std::ostream& C,MATH::complexe& pe);
}
//E1.cpp中
#include<cmath>
#include"E1.h"
double MATH::complexe::module(){
double m,p;
p=p_im*p_im+p_re*p_re;
m=sqrt(p);
return m;
}
const MATH::complexe MATH::operator+(MATH::complexe &p1, MATH::complexe &p2){
return MATH::complexe(p1.Re() +p2.Re(),p1.Im() +p2.Im() );
}
const MATH::complexe MATH::operator*(MATH::complexe &p1, MATH::complexe &p2){
return MATH::complexe(p1.Re()*p2.Re()-p1.Im()*p2.Im(),p1.Re()*p2.Im()+p1.Im()*p2.Re());
}
std::ostream& operator<<(std::ostream& C,MATH::complexe& pe){
C<<pe.Re()<<"+"<<pe.Im()<<"i";
return C;
} 展开
1个回答
展开全部
你的几个运算符重载是用友元重载的,但是你忘记在函数前面写friend了
追问
在经过无数次尝试后,我发现是最后一段代码哪里,如果写成 std::ostream & MATH::operator<<(std::ostream& C,MATH::complexe& pe),就可以了,因为是定义在MATH这个名字空间里的。还是非常谢谢你。
追答
已经用了名字空间了。
其实你只要
using namespace MATH;
using namespace std;
就全对啦
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询