在主函数c++中使用类时出现两个错误
two errors while using classes in main function c++
在主函数中使用类时出现两个错误。
第一个错误-
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) 不能显式调用析构函数。
相关文章:
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- C++哈希表中,两个相同的实现,但一个给出错误
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- C++ 分段错误:11 错误,同时编码将两个数字相加的链接列表
- 在 C++ 中创建两个不同的二叉树时出现分段错误
- 交换两个字符串时访问正确的内存时,我遇到了分段错误
- 试图添加两个矢量,但添加顺序错误
- pair的两个构造函数几乎相同,为什么不生成构建错误?
- 从两个 lambda 的函数返回 lambda 时的链接器错误
- 为什么我在尝试添加两个链表时出现此错误?
- 错误 :"+" 无法添加两个指针
- 表达式未评估为常数两个级别的constexpr函数(编译器错误?)
- 错误:当定义两个带有标头文件的类时,将重新定义类
- 在 c++ 程序中调试链表以添加两个数字: 有人可以解释为什么 l3=l3->next;导致错误?
- 输出错误:两个不同编译器上的不同输出:Prime Cryptarithm USACO
- 在两个类似类之间使用reinterpret_cast时错误
- 如何在两个设备之间与TCP通信时修复错误"连接被拒绝"
- 操作员=在两个std :: chrono :: time_point引起错误之间
- 在发布版本中划分两个双精度的结果错误