简体   繁体   中英

C++ Unscoped Enum Type When Trying to Add Class Constructs

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM