drocktapiff
drocktapiff

Reputation: 117

C++ Overloading Operator issue

So here is my header file:

#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

here is my cpp file:

#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;
}

Now the problem arises when I compile it... I get 1 error and I don't know what it is, what it means, or why and how to fix it.

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

Upvotes: 0

Views: 60

Answers (2)

congusbongus
congusbongus

Reputation: 14622

It means that the linker is trying to look for a definition of Hyperint::Hyperint() but couldn't find it. You need to provide an implementation of it.

Linker errors can be confusing and a lot more so than compiler errors as the names get garbled and you often lose the exact location in your code that generated that error. Let's go through your error message as it contains all the information you need, just presented poorly. I'll bold the important parts.

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

In human terms, Visual Studio is complaining that it's linker encountered an error called LNK2019, which was due to not being able to find the symbol Hyperint::Hyperint(void), whilst it was going through the function Hyperint::operator*=(class Hyperint const &).

First port of call is the error number. This is easily found in a search engine, which gives the following page on MSDN: http://msdn.microsoft.com/en-us/library/799kze2z.aspx

That page describes what the error is, and gives a few examples of what kind of code generates it. This subpage describes the problem that's closer to yours: http://msdn.microsoft.com/en-us/library/x3k07566.aspx

More specifically, it couldn't find an implementation of Hyperint::Hyperint(). In C++ just declaring it (e.g. in a header as Hyperint();) is not enough, you need an implementation (the code in curly braces {}) somewhere, usually in the corresponding cpp file.

Finally, it's saying that it encountered this error whilst processing the Hyperint::operator*=(class Hyperint const &) function. This information is not actually useful in tracking down this error, but it's probably caused by this line:

Hyperint result;

Which creates a Hyperint object and initialises using the no-argument constructor, i.e. Hyperint::Hyperint().

So putting this all together, you have declared and used the no-argument constructor Hyperint::Hyperint() in your header:

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

But you have not implemented it in your cpp file. You'd probably need something like this:

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

Upvotes: 2

ALiX
ALiX

Reputation: 1031

You have declared a constructor Hyperint::Hyperint() that takes no arguments, but you have not provided a definition. In the .cpp file you need to provide one. You could also use a default parameter for the Hyperint::Hyperint(long) constructor (in the header file) instead if that works for your design.

Upvotes: 1

Related Questions