开发者社区> 问答> 正文

C++编译报错:error LNK2019: 无法解析的外部符号 "public?报错

#include<iostream>
#include<cmath>
#include<string.h>


using namespace std;


class myComplex{
   private:
  double real;
  double imag;
    public: 
 myComplex();
 ~myComplex(){}
 myComplex(int a){ real = a; }
 myComplex(int a, int b){ real = a; imag = b;}
 myComplex(myComplex& v){ real = v.real; imag = v.imag; }
 double getReal(){ return this->real;}  //返回复数的实部
 double getImaginary(){ return this->imag; } //返回复数的虚部
 //返回复数的模
 double getModulus(myComplex &num){ 
 double temp; 
 temp = sqrt(num.real*num.real + num.imag*num.imag); 
 return temp;
 }   
 //类对象的赋值
 myComplex& operator=(myComplex& rhs){
 imag = rhs.imag;
 real = rhs.imag;
 return *this;
 }
 //重载+=运算符
 myComplex& operator+=(myComplex& rhs){
 real = real + rhs.real;
 imag = imag + rhs.real;
 return *this;
 }
 //重载-=运算符
 myComplex& operator-=(myComplex& rhs){
 real = real - rhs.real;
 imag = imag -rhs.real;
 return *this;
 }
 //重载*=运算符
 myComplex& operator*=(myComplex& rhs){
 real = real * rhs.real;
 imag = imag * rhs.real;
 return *this;
 }
 //重载/=运算符
 myComplex& operator/=(myComplex& rhs){
 real = real / rhs.real;
 imag = imag /rhs.real;
 return *this;
 }
 //重载+运算符
 friend myComplex operator+(myComplex m, myComplex n){
 myComplex temp;
 temp.real = m.real+n.real;
 temp.imag = m.imag + n.imag;
 return temp;
 }
 //重载-运算符
 friend myComplex operator-(myComplex m, myComplex n){
 myComplex temp;
 temp.real = m.real - n.real;
 temp.imag = m.imag -n.imag;
 return temp;
 }
 //重载*运算符
 friend myComplex operator*(myComplex m, myComplex n){
 myComplex temp;
 temp.real = m.real*n.real - m.imag*n.imag;
 temp.imag = m.imag*n.real + m.real*n.imag;
 return temp;
 }
 //重载/运算符
 friend myComplex operator/(myComplex m, myComplex n){
 myComplex temp;
 temp.real = (m.real*n.real + m.imag*n.imag) / (n.real*n.real+n.imag*n.imag);
 temp.imag = (m.imag*n.real -m.real*n.imag) / (n.real*n.real + n.imag*n.imag);
 return temp;
 }
 //重载<<运算符
 friend ostream& operator<<(ostream &os, myComplex c){
 os << '(' << c.real << ',' << c.imag << ')';
 return os;
 }
 //重载>>运算符
 friend istream& operator>>(istream& is, myComplex& c){
 is >> c.real >> c.imag;
 return is;
 }

};


 

 
 

cpp文件

#include<iostream>
#include<cmath>
#include<string.h>
#include"mycomplex.h"

using namespace std;

int main(){
	myComplex c1;
	myComplex c2;
	cin >> c1 >> c2;
	c2 = c1;
	cout << c1 << endl;
	cout << c2 << endl;
	cout << c1 + c2 << endl;
	cout << c1 - c2 << endl;
	cout << c1 / c2 << endl;
	cout << c1 * c2 << endl;
	return 0;
}



工程截图

错误截图:

感谢您的回答和帮助。

展开
收起
爱吃鱼的程序员 2020-06-08 20:36:59 837 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    或者改为改为myComplex(): real(0.0f),imag(0.0f){};默认初始化为0.0,就更好了。谢谢您,问题已得到解决。

    似乎是因为你的myComplex();没有定义。改为myComplex(){};应该可以通过编译。

    2020-06-08 20:37:17
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像