none 似乎很明显的未声明标识符错误
Undeclared identifier error where none seems apparent
我正在尝试实现一个简单版本的 Conway 生命游戏,它由一个头文件和三个 .cpp 文件组成(两个用于 class 函数,一个用于 main)。这里我包含了我的头文件和两个 class 函数声明文件(编译器对我的 Main.cpp 文件没有问题)。
Game_Of_Life.h
#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;
};
Cell_Class_Functions.cpp
#include "Game_Of_Life.h"
cell::cell(){
state = rand() % 2;
return;
}
void cell::Set_Future (){
if (state == 1){
if (neighbours < 2) future_state = 0;
else if (neighbours == 2 || neighbours == 3) future_state = 1;
else if (neighbours > 3) future_state = 0;
}
else{
if (neighbours == 3) future_state = 1;
}
return;
}
void cell::Update (){
state = future_state;
return;
}
int cell::Current_State (){
return state;
}
void cell::Set_Pos (unsigned int x, unsigned int y){
pos_x = x;
pos_y = y;
return;
}
void Count_Neighbours (cell* A){
neighbours = 0;
if (pos_x > 0) neighbours += A[pos_y * 10 + pos_x - 1].Current_State();
if (pos_x < 9) neighbours += A[pos_y * 10 + pos_x + 1].Current_State();
if (pos_y > 0) neighbours += A[(pos_y - 1) * 10 + pos_x].Current_State();
if (pos_y < 9) neighbours += A[(pos_y + 1) * 10 + pos_x].Current_State();
if (pos_x > 0 && pos_y > 0) neighbours += A[(pos_y - 1) * 10 + pos_x - 1].Current_State();
if (pos_x > 0 && pos_y < 9) neighbours += A[(pos_y + 1) * 10 + pos_x - 1].Current_State();
if (pos_x < 9 && pos_y > 0) neighbours += A[(pos_y - 1) * 10 + pos_x + 1].Current_State();
if (pos_x < 9 && pos_y < 9) neighbours += A[(pos_y + 1) * 10 + pos_x + 1].Current_State();
return;
}
Cell_Array_Class_Functions.cpp
#include "Game_Of_Life.h"
cell_array::cell_array(){
A = (cell**) malloc (sizeof(cell*)*100);
for (unsigned int r = 0; r < 10; r++){
for (unsigned int c = 0; c < 10; c++){
*A[r * 10 + c].Set_Pos(r,c);
}
}
return;
}
void cell_array::Update_Array(){
for (unsigned int r = 0; r < 10; r++){
for (unsigned int c = 0; c < 10; c++){
*A[r * 10 + c].Update();
}
}
}
void cell_array::Set_Future_Array(){
for (unsigned int r = 0; r < 10; r++){
for (unsigned int c = 0; c < 10; c++){
*A[r * 10 + c].Count_Neighbours(A);
*A[r * 10 + c].Set_Future();
}
}
return;
}
void cell_array::Print_Array(){
cout << "\n";
for (unsigned int r = 0; r < 10; r++){
for (unsigned int c = 0; c < 10; c++)cout << *A[r * 10 + c].Current_State() << " ";
cout << "\n";
}
return;
}
据我了解,由于我将头文件包含在 class 声明中,那么我应该能够通过之前声明的函数访问 class 的私有成员class.
基本上错误报告看起来像
Error C2065 'item' : undeclared identifier
从 cell class.
调用的每个私有成员都会出现此错误
我做错了什么?
我在你的代码中没有看到 item
这个词。但是,您需要修复:
void Count_Neighbours (cell* A){ ... }
应该是:
void cell::Count_Neighbours (cell* A){ ... }
另外,在你的Cell_Array_Class_Functions.cpp
你需要调整你的功能。
.
运算符用在对象上,references.You 必须先推导它才能获得引用。即:
(*A[r * 10 + c]).Set_Pos(r,c);
或者,您可以使用(这是首选且更易于阅读的方式):
A[r * 10 + c]->Set_Pos(r,c);
两者是等价的
我正在尝试实现一个简单版本的 Conway 生命游戏,它由一个头文件和三个 .cpp 文件组成(两个用于 class 函数,一个用于 main)。这里我包含了我的头文件和两个 class 函数声明文件(编译器对我的 Main.cpp 文件没有问题)。
Game_Of_Life.h
#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;
};
Cell_Class_Functions.cpp
#include "Game_Of_Life.h"
cell::cell(){
state = rand() % 2;
return;
}
void cell::Set_Future (){
if (state == 1){
if (neighbours < 2) future_state = 0;
else if (neighbours == 2 || neighbours == 3) future_state = 1;
else if (neighbours > 3) future_state = 0;
}
else{
if (neighbours == 3) future_state = 1;
}
return;
}
void cell::Update (){
state = future_state;
return;
}
int cell::Current_State (){
return state;
}
void cell::Set_Pos (unsigned int x, unsigned int y){
pos_x = x;
pos_y = y;
return;
}
void Count_Neighbours (cell* A){
neighbours = 0;
if (pos_x > 0) neighbours += A[pos_y * 10 + pos_x - 1].Current_State();
if (pos_x < 9) neighbours += A[pos_y * 10 + pos_x + 1].Current_State();
if (pos_y > 0) neighbours += A[(pos_y - 1) * 10 + pos_x].Current_State();
if (pos_y < 9) neighbours += A[(pos_y + 1) * 10 + pos_x].Current_State();
if (pos_x > 0 && pos_y > 0) neighbours += A[(pos_y - 1) * 10 + pos_x - 1].Current_State();
if (pos_x > 0 && pos_y < 9) neighbours += A[(pos_y + 1) * 10 + pos_x - 1].Current_State();
if (pos_x < 9 && pos_y > 0) neighbours += A[(pos_y - 1) * 10 + pos_x + 1].Current_State();
if (pos_x < 9 && pos_y < 9) neighbours += A[(pos_y + 1) * 10 + pos_x + 1].Current_State();
return;
}
Cell_Array_Class_Functions.cpp
#include "Game_Of_Life.h"
cell_array::cell_array(){
A = (cell**) malloc (sizeof(cell*)*100);
for (unsigned int r = 0; r < 10; r++){
for (unsigned int c = 0; c < 10; c++){
*A[r * 10 + c].Set_Pos(r,c);
}
}
return;
}
void cell_array::Update_Array(){
for (unsigned int r = 0; r < 10; r++){
for (unsigned int c = 0; c < 10; c++){
*A[r * 10 + c].Update();
}
}
}
void cell_array::Set_Future_Array(){
for (unsigned int r = 0; r < 10; r++){
for (unsigned int c = 0; c < 10; c++){
*A[r * 10 + c].Count_Neighbours(A);
*A[r * 10 + c].Set_Future();
}
}
return;
}
void cell_array::Print_Array(){
cout << "\n";
for (unsigned int r = 0; r < 10; r++){
for (unsigned int c = 0; c < 10; c++)cout << *A[r * 10 + c].Current_State() << " ";
cout << "\n";
}
return;
}
据我了解,由于我将头文件包含在 class 声明中,那么我应该能够通过之前声明的函数访问 class 的私有成员class.
基本上错误报告看起来像
Error C2065 'item' : undeclared identifier
从 cell class.
调用的每个私有成员都会出现此错误我做错了什么?
我在你的代码中没有看到 item
这个词。但是,您需要修复:
void Count_Neighbours (cell* A){ ... }
应该是:
void cell::Count_Neighbours (cell* A){ ... }
另外,在你的Cell_Array_Class_Functions.cpp
你需要调整你的功能。
.
运算符用在对象上,references.You 必须先推导它才能获得引用。即:
(*A[r * 10 + c]).Set_Pos(r,c);
或者,您可以使用(这是首选且更易于阅读的方式):
A[r * 10 + c]->Set_Pos(r,c);
两者是等价的