我在主函数中使用类时遇到两个错误。
第一个错误-
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/