Pregunta

the following code is meant to add two LargeNum type class constructs and add them.

class LargeNum
{

private:
    NumNode* first;
    NumNode* last;

    bool isNeg;

public:

    int value;
    NumNode* next;

    //todo constructor, inital point

    //constructor
    LargeNum(int value);
    LargeNum(string value);
    //destructor
    ~LargeNum();

    //members   
    void Append(int value);
    string ToString()const;

    const LargeNum LargeNum::operator+ (const LargeNum& ln2)
    {

        LargeNum result = value + ln2.value;

        return result;

    }

    //LargeNum add(LargeNum val1, LargeNum val2);

};

When I try and add the two in the main.cpp then I get integral or unscoped enum type error and it references the second pointer. See below.

LargeNum Result_1 = L1 + L2;

with the error falling on l2. Thoughts?

¿Fue útil?

Solución

Not sure exactly what is the error that you get, however, the code below compiles just fine (a simplified version of your class)

#include <iostream>
#include <string>

using namespace std;

class LargeNum
{

public:

int value;

//todo constructor, inital point

//constructor
LargeNum(int value):value(value){};
LargeNum(string value){};
//destructor
~LargeNum(){};

//members   

const LargeNum operator+ (const LargeNum& ln2)
{

    LargeNum result = value + ln2.value;

    return result;

}
};

int main()
{
    LargeNum L1=4;
    LargeNum L2=5;
    LargeNum L3=L1+L2;

    cout <<L3.value;

}

and outputs 9 as a result. Btw, you don't need the extra LargeNum:: qualification when declaring operator+(). Check if it compiles without, if not, please post the whole code (with implementation) in a single file.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top