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

标签 c++ class program-entry-point

我在主函数中使用类时遇到两个错误。

第一个错误-

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

关于c++ - 在主函数 c++ 中使用类时出现两个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36378305/

相关文章:

c# - 你如何在 C# 中为我的撤消重做类只分配一个指针

c# - Main() 的默认访问说明符

c++ - 在 main 之前调试对全局变量的赋值

c - main 的所有命令行参数

jquery - 如果 body 有此类,则将此内容放在 #mydiv 中,否则将其他内容放在 #mydiv 中

python - 为什么在父类中改变 'x'的值只会改变一个子类的值?

c++ - 为什么这个异常没有被跨 DLL 捕获?

c++ - std::vector 元素的破坏顺序

c++ - 用零 eigen3 c++ 替换所有负元素

c++ - 将文本文件读入矩阵