嗨,堆栈溢出的人,
我正在编程一个计算复数总和的程序,并在修复退出其他(编译)错误之后我遇到了链接器错误,我似乎无法解决。 我几乎看不到它的用途,但在尝试了一些之后,看了上网并在退出一段时间后得到了同样的错误我决定在这里发布。
注意:“std_lib_facilities.h”是一个库,在我使用的书中使用,并添加了不同的库,如,等等。
这是我得到的链接器错误,我删除了长地址:
Severity Code Description Project File Line Suppression State
Error LNK1120 3 unresolved externals <adress> project.exe 1
Error LNK2019 unresolved external symbol "public: __thiscall Complex::Complex(void)" (??0Complex@@QAE@XZ) referenced in function "public: class Complex __thiscall Complex::Add(class Complex)" (?Add@Complex@@QAE?AV1@V1@@Z) <adress> Complex.obj 1
Error LNK2001 unresolved external symbol "public: __thiscall Complex::Complex(void)" (??0Complex@@QAE@XZ) <adress> Numbers.obj 1
Error LNK2019 unresolved external symbol "public: __thiscall Complex::~Complex(void)" (??1Complex@@QAE@XZ) referenced in function "public: class Complex __thiscall Complex::Add(class Complex)" (?Add@Complex@@QAE?AV1@V1@@Z) <adress> Complex.obj 1
Error LNK2019 unresolved external symbol "public: __thiscall Complex::Complex(double,double)" (??0Complex@@QAE@NN@Z) referenced in function _main <adress> Numbers.obj 1
我很抱歉代码加载。是否可以下次将其上传到Github?
亲切的问候。
主程序:
#include "Complex.h"
#include "std_lib_facilities.h"
int main() {
double a1 = 0; //First real number.
double b1 = 0; //First imaginary.
double a2 = 0; //Second real number.
double b2 = 0; //Second imaginary.
char choice;
cout << "Please enter a number for the first real part:";
cin >> a1;
cout << "\nPlease enter a number for the first imaginary number:";
cin >> b1;
Complex complex1(a1, b1);
complex1.print();
cout << "Please enter a number for the first real part:";
cin >> a2;
cout << "\nPlease enter a number for the first imaginary number:";
cin >> b2;
Complex complex2(a2, b2);
complex2.print();
cout << "\nPlease choose or you want to add (+), subtract (-) or multiply (*) the two answers by entering the right character:";
cin >> choice;
Complex c;
if (choice == '+') {
c = complex1.Add(complex2);
cout << "\nResult for addition is: (" << c.getReal() << ")+(" << c.getImag() << ")i\n";
}
else if (choice == '-') {
c = complex1.Sub(complex2);
cout << "\nResult for subtraction is: (" << c.getReal() << ")+(" << c.getImag() << ")i\n";
}
else if (choice == '*') {
c = complex1.Mult(complex2);
cout << "\nResult for multiplication is: (" << c.getReal() << ")+(" << c.getImag() << ")i\n";
}
keep_window_open();
return 0;
}
Complex.cpp
#include "Complex.h"
#include "std_lib_facilities.h"
void Complex::setA(double a) { //Bind attribute a to a.
this->a = a;
}
void Complex::setB(double b) { //Bind attribute b to b.
this->b = b;
}
void Complex::eaqualize(double r, double i) { //Eaqualize a and b to 0 and bind r and i to them.
a = b = 0;
a = r;
b = i;
}
void Complex::objects(Complex &complex) { //Define objects for a and b.
a = complex.a;
b = complex.b;
}
Complex Complex::Add(Complex c) {
Complex Add;
Add.a = a + c.a;
Add.b = b + c.b;
return Add;
}
Complex Complex::Sub(Complex c) {
Complex Sub;
Sub.a = a - c.a;
Sub.b = b - c.b;
return Sub;
}
Complex Complex::Mult(Complex c) {
Complex Mult;
Mult.a = a * c.a - b * c.b;
Mult.b = a * c.b - c.a*b;
return Mult;
}
void Complex::print() {
cout << a << "+" << b << "i" << endl << endl;
}
double Complex::getReal() const {
return a;
}
double Complex::getImag() const {
return b;
}
void Complex::setReal(double a, double re) {
this->a = re;
}
void Complex::setImag(double b, double im) {
this->b = im;
}
Complex.h
#ifndef _Complex
#define _Complex
class Complex {
//Attributes
double a; //Real input
double b; //Imaginairy input
double r; //Real converted
double i; //Imaginairy converted
double c;
public:
Complex();
Complex(double a, double b); //Constructor
//Complex(double c); //Another constructor
//Methode
void setA(double a);
void setB(double b);
void eaqualize(double r, double i);
void objects(Complex &complex);
Complex Add(Complex c);
Complex Sub(Complex c);
Complex Mult(Complex c);
void print();
double getReal(void) const;
double getImag(void) const;
void setReal(double a, double re);
void setImag(double b, double im);
~Complex(); //Destructor
#endif;
};
答案 0 :(得分:0)
链接器消息相当清楚,不是吗?也许如果我删除一些装饰品?
未解决的外部符号&#34;复杂::复杂(无效)&#34;
未解决的外部符号&#34;复杂::复杂(无效)&#34;
未解决的外部符号&#34;复杂::〜复杂(无效)&#34;
未解决的外部符号&#34;复杂::复杂(双,双)&#34;
您缺少Complex
(已报告两次)的默认构造函数的定义,Complex
的析构函数以及Complex
的构造函数,其中两个double
为参数。传统上,这些定义将与Complex.cpp
的其他方法的定义一起出现在Complex
中,除非它们足够简单,可以在头文件中提供内联定义。
根据您定义的方法,看起来您不倾向于内联定义。因此,请将Complex::Complex()
,Complex::Complex(double,double)
和Complex::~Complex()
的定义添加到Complex.cpp
。
答案 1 :(得分:0)
好吧,我已经编辑了代码并使代码更短,但看来我仍然遇到一些链接器错误。 回到JaMiT的问题:不,链接器错误对我来说不是很清楚。编译器错误更清晰,并且提供错误所在的行。 例如,我没有得到的是我再次得到了析构函数的链接器错误。
Error LNK2019 unresolved external symbol "public: __thiscall Complex::~Complex(void)" (??1Complex@@QAE@XZ) referenced in function "public: class Complex __thiscall Complex::operator*(class Complex const &)" (??DComplex@@QAE?AV0@ABV0@@Z <address on disk> Complex.obj 1
所以简而言之:LNK2019在Complex :: ~~ Complex(void)处未解析的外部符号
但是,如果我的class.cpp文件中已经存在我的header / .h文件中的析构函数,为什么还要重新声明呢?