This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(32个答案)
上个月关闭。
我正在尝试编写一个对三次多项式执行运算的Cubic类,当尝试重载+运算符以返回新的Cubic对象时,它给了我LNK2019错误:
在函数“ public:class Cubic const ____call call Cubic :: operator +(class Cubic)”中引用的“未解析的外部符号“ public:__thiscall Cubic :: Cubic(void)”(?? 0Cubic @@ QAE @ XZ)”(?? HCubic @ @QAE?BV0 @ V0 @@ Z)”
我尝试查看我的函数声明是否不同于我的定义,但是它们都相同。我相信问题出在重载运算符上,因为我尝试使用
Cubic.h文件
立方
测试文件
预期结果应为
但您从未定义过该构造函数(至少在您显示的任何代码中都没有定义)。
您需要定义默认的内联构造函数,如下所示:
或在其他地方提供单独的定义:
您还可以将“执行某些操作”代码添加到内联定义中!如果定义较小(一两行),大多数编码器会这样做,但对于更复杂的代码,则将定义分开。
编辑:或者,正如@Scheff在注释中指出的那样,您可以使用以下方式显式定义默认构造函数:
(32个答案)
上个月关闭。
我正在尝试编写一个对三次多项式执行运算的Cubic类,当尝试重载+运算符以返回新的Cubic对象时,它给了我LNK2019错误:
在函数“ public:class Cubic const ____call call Cubic :: operator +(class Cubic)”中引用的“未解析的外部符号“ public:__thiscall Cubic :: Cubic(void)”(?? 0Cubic @@ QAE @ XZ)”(?? HCubic @ @QAE?BV0 @ V0 @@ Z)”
我尝试查看我的函数声明是否不同于我的定义,但是它们都相同。我相信问题出在重载运算符上,因为我尝试使用
rhs.coefficient[i] += coefficient[i]
修改每个系数并返回rhs,并且效果很好。我想要返回一个新的Cubic的原因是因为它看起来更正确,而且还因为它使实现-运算符更加容易。Cubic.h文件
#include <iostream>
using namespace std;
class Cubic
{
public:
// Default constructor
Cubic();
// Predetermined constructor
Cubic(int degree3, int degree2, int degree1, int degree0);
// Return coefficient
int getCoefficient(int degree) const;
// Addition op
const Cubic operator+(Cubic rhs);
// Output operators
friend ostream& operator<<(ostream& outStream, const Cubic& cubic);
private:
int coefficient[4];
};
立方
#include "Cubic.h"
#include <iostream>
using namespace std;
Cubic::Cubic(int degree3, int degree2, int degree1, int degree0)
{
coefficient[3] = degree3;
coefficient[2] = degree2;
coefficient[1] = degree1;
coefficient[0] = degree0;
}
int Cubic::getCoefficient(int degree) const
{
return coefficient[degree];
}
const Cubic Cubic::operator+(Cubic rhs)
{
Cubic result;
for (int i = 3; i >= 0; i--)
{
result.coefficient[i] = coefficient[i] + rhs.coefficient[i];
}
return result;
}
ostream& operator<<(ostream& outStream, const Cubic& cubic) {
outStream << showpos << cubic.getCoefficient(3) << "x^(3)"
<< cubic.getCoefficient(2) << "x^(2)"
<< cubic.getCoefficient(1) << "x"
<< cubic.getCoefficient(0);
return outStream;
}
测试文件
#include "Cubic.h"
#include <iostream>
using namespace std;
int main()
{
Cubic lhs(3, 1, -4, -1);
Cubic rhs(-1, 7, -2, 3);
/* TESTS */
cout << "Addition using + operator" << endl;
cout << lhs + rhs << endl;
return 0;
}
预期结果应为
+2x^(3)+8x^(2)-6x+2
。 最佳答案
您的问题是您为Cubic
类声明了默认的构造函数,在这里:
// Default constructor
Cubic();
但您从未定义过该构造函数(至少在您显示的任何代码中都没有定义)。
您需要定义默认的内联构造函数,如下所示:
// Default constructor
Cubic() { } // This provides a definition, but it doesn't DO anything.
或在其他地方提供单独的定义:
Cubic::Cubic()
{
// Do something here...
}
您还可以将“执行某些操作”代码添加到内联定义中!如果定义较小(一两行),大多数编码器会这样做,但对于更复杂的代码,则将定义分开。
编辑:或者,正如@Scheff在注释中指出的那样,您可以使用以下方式显式定义默认构造函数:
// Default constructor
Cubic() = default; // Will do minimal required construction code.
关于c++ - 链接CPP文件进行测试时出现LNK2019错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58663363/
10-10 13:42