【发布时间】:2014-04-14 09:17:02
【问题描述】:
这是我的头文件:
#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 个错误,但我不知道它是什么,它意味着什么,或者为什么以及如何解决它。
错误 1 错误 LNK2019: 函数“public: class Hyperint & __thiscall Hyperint::operator*=(类 Hyperint const &)" (??XHyperint@@QAEAAV0@ABV0@@Z) C:\Users\Drock\documents\visual studio 2013\Projects\A3-Attempt\A3-Attempt\Hyperint.obj A3-Attempt
【问题讨论】:
-
在您的 .cpp 文件中实现无参数
Hyperint()构造函数。似乎您只在标题中声明了它。 -
所以我必须在我的 cpp 文件中声明一个就是你在说什么?
-
不,我的意思是你必须在 C++ 文件中为无参数构造函数提供一个实现。您不必移动/移除任何东西。
-
非常感谢我成功了!
-
如果您的问题得到解决,接受答案将是一种很好的礼仪。让这里的人保持参与和积极性:-)。
标签: c++ operator-keyword