【问题标题】:C++ Overloading Operator issueC ++重载运算符问题
【发布时间】: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


【解决方案1】:

这意味着链接器正在尝试查找Hyperint::Hyperint() 的定义但找不到。您需要提供它的实现。

链接器错误可能比编译器错误更令人困惑,因为名称会出现乱码,并且您经常会丢失代码中产生该错误的确切位置。让我们看一下您的错误消息,因为它包含您需要的所有信息,只是呈现得很差。我会加粗重要部分。

错误 1 ​​error LNK2019:函数中引用了无法解析的外部符号“public: __thiscall Hyperint::Hyperint(void)” (??0Hyperint@@QAE@XZ) "public: class Hyperint & __thiscall Hyperint::operator*=(class Hyperint const &)" (??XHyperint@@QAEAAV0@ABV0@@Z) C:\Users\Drock\documents\visual工作室 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++ 中,仅声明它(例如在标头中声明为 Hyperint();)是不够的,您需要一个实现(大括号 {} 中的代码),通常在相应的 cpp 文件中。

最后,它在处理Hyperint::operator*=(class Hyperint const &amp;) 函数时遇到了这个错误。此信息实际上对跟踪此错误没有用处,但可能是由以下行引起的:

Hyperint result;

它创建一个Hyperint 对象并使用无参数构造函数进行初始化,即Hyperint::Hyperint()

综上所述,您已经在标题中声明并使用了无参数构造函数Hyperint::Hyperint()

class Hyperint
{
public:
Hyperint();    // < this line here
Hyperint(long a);
~Hyperint(void);
// ...
};

但是你还没有在你的 cpp 文件中实现它。你可能需要这样的东西:

Hyperint::Hyperint()
{
    // some code goes here, if required
}

【讨论】:

    【解决方案2】:

    您已经声明了一个不带参数的构造函数Hyperint::Hyperint(),但您还没有提供定义。在 .cpp 文件中,您需要提供一个。如果适用于您的设计,您也可以为 Hyperint::Hyperint(long) 构造函数(在头文件中)使用默认参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多