Search code examples
c++classprogram-entry-point

two errors while using classes in main function c++


I have two errors while using classes in my main function.

first error -

error C2227: left of '->digitarray' must point to class/struct/union/generic type

The second error is -

error C2675: unary '~' : 'game' does not define this operator or a conversion to a type acceptable to the predefined operator

The header file -

class game{
private:
    int cows();
    int bulls();
    bool game_over = false;

public:
    int x;
    number *user, *computer;
    game();
    ~game();
    game(const number,const number);
    void play();
};

The main file -

int main(){
    game();
    for (int i = 0; i < SIZE; i++){
        cout << game::computer->digitarray[i].value;
    } 

    ~game();

}

And the "number" header file -

#define SIZE 4

class number{
private:
public:
    digit digitarray[SIZE];
    number();
    void numscan();
    void randomnum();
    int numreturn(int);
    void numprint();
};

Solution

  • The fix is very simple, declare a variable of type game:

    int main(){
        game g;
          // ^^
        for (int i = 0; i < SIZE; i++){
            cout << g.computer->digitarray[i].value;
                 // ^^
        } 
    
        // ~game(); <<< You don't need this or g.~game();
    }   // <<< That's done automatically here