这是我的头文件:
#pragma once
#ifndef HYPERINT_H
#define HYPERINT_H
#include <iostream>
#include <vector>
class Hyperint
{
public:
Hyperint();
Hyperint(long a);
~Hyperint(void);
Hyperint & operator*= (const Hyperint & right);
std::vector<int> hyperintVector;
};
Hyperint operator* (const Hyperint & left, const Hyperint &right);
#endif
这是我的cpp文件:
#include "stdafx.h"
#include "Hyperint.h"
using namespace std;
Hyperint::Hyperint(long a)
{
//vector<int> hyperint;
int b = a;
while (b != 0){
int h = b % 10;
this->hyperintVector.push_back(h);
b = b / 10;
}
}
Hyperint::~Hyperint()
{
}
Hyperint operator*(const Hyperint & left, const Hyperint & right){
vector<int> leftVec = left.hyperintVector;
vector<int> rightVec = right.hyperintVector;
vector<int> resultVector;
Hyperint result;
int carry = 0;
int counter1 = 0;
for (vector<int>::const_iterator it = leftVec.begin(); it != leftVec.end(); ++it){
int counter2 = 0;
int totalOperand = 0;
for (vector<int>::const_iterator it2 = rightVec.begin(); it2 != rightVec.end(); ++it2){
double pow2 = pow(10, counter2);
totalOperand += ((*it2) * ((int) pow2)) * (*it);
++counter2;
}
totalOperand += carry;
int store = totalOperand % 10;
resultVector.push_back(store);
carry = totalOperand / 10;
++counter1;
}
while (carry != 0){
int putIn = carry % 10;
resultVector.push_back(putIn);
carry /= 10;
}
result.hyperintVector = resultVector;
return result;
}
Hyperint & Hyperint::operator*=(const Hyperint & right){
vector<int> rightVec = right.hyperintVector;
//vector<int> leftVec = this->hyperintVector;
vector<int> resultVector;
Hyperint theResult;
int carry = 0;
int counter1 = 0;
for (vector<int>::const_iterator it = (this->hyperintVector).begin(); it != (this->hyperintVector).end(); ++it){
int counter2 = 0;
int totalOperand = 0;
for (vector<int>::const_iterator it2 = rightVec.begin(); it2 != rightVec.end(); ++it2){
double pow2 = pow(10, counter2);
totalOperand += ((*it2) *((int)pow2)) * (*it);
++counter2;
}
totalOperand += carry;
int store = totalOperand % 10;
resultVector.push_back(store);
carry = totalOperand / 10;
++counter1;
}
while (carry != 0){
int putIn = carry % 10;
resultVector.push_back(putIn);
carry = carry/10;
}
(this->hyperintVector) = resultVector;
return *this;
}
现在,当我编译它时出现问题...我收到 1 个错误,我不知道它是什么、它意味着什么,或者为什么以及如何修复它。
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Hyperint::Hyperint(void)" (??0Hyperint@@QAE@XZ) referenced in function "public: class Hyperint & __thiscall Hyperint::operator*=(class Hyperint const &)" (??XHyperint@@QAEAAV0@ABV0@@Z) C:\Users\Drock\documents\visual studio 2013\Projects\A3-Attempt\A3-Attempt\Hyperint.obj A3-Attempt
最佳答案
这意味着链接器正在尝试寻找 Hyperint::Hyperint()
的定义,但找不到。您需要提供它的实现。
链接器错误可能会令人困惑,而且比编译器错误更令人困惑,因为名称会出现乱码,而且您经常会丢失代码中产生该错误的确切位置。让我们检查一下您的错误消息,因为它包含您需要的所有信息,只是呈现不佳。我会将重要部分加粗。
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Hyperint::Hyperint(void)" (??0Hyperint@@QAE@XZ) referenced in function "public: class Hyperint & __thiscall Hyperint::operator*=(class Hyperint const &)" (??XHyperint@@QAEAAV0@ABV0@@Z) C:\Users\Drock\documents\visual studio 2013\Projects\A3-Attempt\A3-Attempt\Hyperint.obj A3-Attempt
用人话来说,Visual Studio 提示它的链接器遇到名为 LNK2019 的错误,这是由于无法找到符号 Hyperint::Hyperint(void),同时它正在通过函数 Hyperint::operator*=(class Hyperint const &)。
第一个停靠点是错误号。这很容易在搜索引擎中找到,它在 MSDN 上提供以下页面:http://msdn.microsoft.com/en-us/library/799kze2z.aspx
该页面描述了错误是什么,并给出了生成错误的代码类型的一些示例。此子页面描述了更接近您的问题:http://msdn.microsoft.com/en-us/library/x3k07566.aspx
更具体地说,它找不到 Hyperint::Hyperint()
的实现。在 C++ 中,仅声明它(例如,在 header 中作为 Hyperint();
)是不够的,您通常需要在某处实现(大括号 {}
中的代码)在相应的cpp文件中。
最后,它说它在处理 Hyperint::operator*=(class Hyperint const &)
函数时遇到了这个错误。此信息对于跟踪此错误实际上没有用,但它可能是由以下行引起的:
Hyperint result;
它创建一个 Hyperint
对象并使用无参数构造函数初始化,即 Hyperint::Hyperint()
。
综上所述,您已经在 header 中声明并使用了无参数构造函数 Hyperint::Hyperint()
:
class Hyperint
{
public:
Hyperint(); // < this line here
Hyperint(long a);
~Hyperint(void);
// ...
};
但是你还没有在你的cpp文件中实现它。您可能需要这样的东西:
Hyperint::Hyperint()
{
// some code goes here, if required
}
关于C++ 重载运算符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22314238/