C++ 未声明的标识符错误,其中似乎没有明显的标识符错误
我正在尝试实现一个简单版本的Conway's Game of Life,其中包括一个头文件和三个.cpp文件(两个用于类函数,一个用于主函数)。这里我包含了头文件和两个类函数声明文件(编译器对Main.cpp文件没有问题) 生命的游戏.hC++ 未声明的标识符错误,其中似乎没有明显的标识符错误,c++,class,conways-game-of-life,undeclared-identifier,C++,Class,Conways Game Of Life,Undeclared Identifier,我正在尝试实现一个简单版本的Conway's Game of Life,其中包括一个头文件和三个.cpp文件(两个用于类函数,一个用于主函数)。这里我包含了头文件和两个类函数声明文件(编译器对Main.cpp文件没有问题) 生命的游戏.h #include <iostream> #include <cstdlib> #include <time.h> using namespace std; class cell{ public:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
class cell{
public:
cell();
int Current_State(); // Returns state (1 or 0)
void Count_Neighbours(cell* A); // Counts the number of living cells in proximity w/o wraparound
void Set_Future(); // Determines the future value of state from # of neighbbours
void Update(); // Sets state to value of future state
void Set_Pos(unsigned int x, unsigned int y); // Sets position of cell in the array for use in counting neighbours
private:
int state;
int neighbours;
int future_state;
int pos_x;
int pos_y;
};
class cell_array{
public:
cell_array();
void Print_Array(); // Prints out the array
void Update_Array(); // Updates the entire array
void Set_Future_Array(); // Sets the value of the future array
private:
cell** A;
};
对于从单元格调用的每个私有成员类,都会出现此错误
我做错了什么?我在代码中的任何地方都看不到
项。但是,您需要修复:
void Count_Neighbours (cell* A){ ... }
应该是:
void cell::Count_Neighbours (cell* A){ ... }
此外,在您的单元数组\u类函数.cpp中,您需要调整函数
运算符用于对象和引用。必须首先遵从它才能获得引用。即:
(*A[r*10+c])。设置位置(r,c)代码>
或者,您可以使用(这是首选且更易于阅读的方式):
A[r*10+c]->Set_Pos(r,c)代码>
这两者是等效的。基本上错误报告看起来像
基本上,问题在代码中的某个地方。这个答案够好吗?你肯定觉得你的问题已经足够好了<代码>帮助Plz?
如果您真的需要帮助,您应该显示准确的错误消息及其所指向的代码行。但是你没有,这向我表明你并不真的需要帮助。你使用malloc
是不正确的。Malloc只能用于豆荚。使用vector
。@M.M但指针(或其数组)是POD。不会使以下代码变得更少;)第二个是最好的,更习惯的C++。如果你滥用德墨忒尔定律,以至于有了name1->ptr2->ptr3.member4->member5[index]
,那么第一个符号会变得非常笨拙;这比(*(*(*name1.ptr2.ptr3.member4.member5[index]
可读性好得多(尽管即使使用箭头操作符也不是很好)。
Error C2065 'item' : undeclared identifier
void Count_Neighbours (cell* A){ ... }
void cell::Count_Neighbours (cell* A){ ... }