在主函数c++中使用类时出现两个错误

two errors while using classes in main function c++

本文关键字:错误 两个 c++ 函数      更新时间:2023-10-16

在主函数中使用类时出现两个错误。

第一个错误-

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

第二个错误是-

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

头文件-

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();
};

主文件-

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

和"数字"头文件-

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

修复非常简单,声明一个类型为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

您的代码出现以下问题。

1) 您还没有创建对象并尝试访问类成员,这只能对静态类成员执行。

2) 不能显式调用析构函数。