您好,我正在处理的 C++ 问题遇到了这个问题。
这是代码
Cell.h
#ifndef CELL_H
#define CELL_H
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Cell
{
private:
int level;
int row;
int column;
//declares a variable called ptrFunction_array which is an array of 3 function pointers.
typedef void (*ptrFunction[])(void);
static void function1()
{
cout << "I'm function 1";
}
static void function2()
{
cout << "I'm function 2";
}
static void function3()
{
cout << "I'm function 3";
}
public:
Cell(int currentLevel, int currentRow, int currentColumn)
{
level = currentLevel;
row = currentRow;
column = currentColumn;
ptrFunction = new *fArray[3];
fArray[0] = function1();
fArray[1] = function2();
fArray[2] = function3();
}
virtual ~Cell();
void tick()
{
int randomNumber = rand() % 3;
cout << "Cell(" << level << ", " << row << ", " << column << ") ";
fArray[randomNumber];
}
};
#endif // CELL_H
Main.cpp
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "Cell.h"
using namespace std;
Cell ****myArray;
int main()
{
int level = 0;
int row = 0;
int column = 0;
char userInput = 'y';
srand (time(NULL));
do
{
cout << "Please input the amount of levels: ";
cin >> level;
cout << "Please input the amount of rows: ";
cin >> row;
cout << "Please input the amount of columns: ";
cin >> column;
cout << endl;
myArray = new Cell *** [level];
// Set random number to the elements of the array
for (int currentLevel = 0; currentLevel < level; currentLevel++)
{
myArray [currentLevel] = new Cell ** [row];
for (int currentRow = 0; currentRow < row; currentRow++)
{
myArray [currentLevel][currentRow] = new Cell * [column];
for (int currentColumn = 0; currentColumn < column; currentColumn++)
{
myArray [currentLevel][currentRow][currentColumn] = new Cell (currentLevel, currentRow, currentColumn);
myArray [currentLevel][currentRow][currentColumn] -> tick();
cout << " ";
}
cout << endl;
}
cout << endl;
}
cout << "Do you want to try again? (y / n) ";
cin >> userInput;
cout << endl;
if ((userInput == 'y') || (userInput == 'Y'))
{
for (int currentLevel = 0; currentLevel < level; currentLevel++)
{
for (int currentRow = 0; currentRow < row; currentRow++)
{
for (int currentColumn = 0; currentColumn < column; currentColumn++)
{
delete[] myArray[currentLevel][currentRow][currentColumn];
}
delete[] myArray[currentLevel][currentRow];
}
delete[] myArray[currentLevel];
}
delete[] myArray;
myArray = NULL;
}
}while (userInput != 'n');
return 0;
}
我注意到我的 fArray 不在范围内。 ptrFunction = new *fArray[3];
行是我的错误所在。我最近开始学习 C++,所以我正在尝试理解为什么我的 typedef void (*ptrFunction[])(void);
没有正确初始化 fArray
对于我的程序。我的程序的目标是能够创建一个 3 维数组并能够指向 Cell 对象并能够跟踪位置 x,y,z。
为什么会出现这样的错误?
最佳答案
我现在将忽略四星指针,并坚持那些给OP带来最直接悲伤的东西。
快速浏览:
Cell(int currentLevel, int currentRow, int currentColumn)
{
level = currentLevel;
row = currentRow;
column = currentColumn;
这里还不错。但是...
ptrFunction = new *fArray[3];
这表示将一个新分配的由 3 个 fArray
组成的数组分配给变量 ptrFunction
,该变量必须已经存在,但不存在。这里的问题是 ptrFunction
已被定义为类型,而不是变量。 fArray
不是一种类型。
fArray[0] = function1();
fArray[1] = function2();
fArray[2] = function3();
使用fArray
作为变量,让这里的问题变得更加清晰。
}
Cell 需要看起来更像这样,但不完全是这样。稍后会详细介绍。
Cell(int currentLevel, int currentRow, int currentColumn)
{
level = currentLevel;
row = currentRow;
column = currentColumn;
ptrFunction * fArray = new ptrFunction[3];
现在,fArray
是一个变量,它指向一个或多个ptrFunction
类型的对象(但是ptrFunction
的定义有些损坏),并将 fArray
指向三个 ptrFunction
。从技术上讲,它指向三个 ptrFunction
中的第一个。
fArray[0] = function1();
fArray[1] = function2();
fArray[2] = function3();
}
现在我们有一个 fArray
,但它是 local variable它只存在于 Cell
构造函数的大括号之间。当构造函数存在时,指针就会消失。分配的内存没有并且丢失。如果没有 fArray
指向它,您就无法轻松找到它并使用或删除它。 fArray
需要更宽的 scope这样 A) 内存不会丢失,B) 以便 tick
和 Cell
的其他成员可以看到它。
class Cell
{
private:
ptrFunction * fArray;
int level;
在构造函数中:
Cell(int currentLevel, int currentRow, int currentColumn)
{
level = currentLevel;
row = currentRow;
column = currentColumn;
fArray = new ptrFunction[3];
这修复了找不到fArray
的问题。
我的建议是让一个 Cell
工作,然后尝试让 Cell
的一维数组工作。当你有一个维度时,然后尝试两个维度。您可能会发现这就是您所需要的。
编辑
忘记提及这一点:指向成员函数的函数指针是绝对的咒骂,删除才能正确。 Here is a page on common pitfalls and how to avoid them.
以下是我们如何在现代 C++ 的此时此地避免这种蓝 Sprite 的方法: std::bind和 std::function 。链接文档页面底部的教程可能比我更好地描述了如何将它们用于简单的情况。
关于c++ - “fArray”未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330046/