【发布时间】:2014-07-23 00:42:13
【问题描述】:
我正在为元胞自动机编写代码,我需要一个进化函数来计算时间步后自动机的状态。 我选择将此函数称为 evol,为了测试它,我在 C++ 中创建了一个基本函数。不幸的是,它无法编译,因为编译器无法理解我需要它来返回一个数组。这是代码:
#include <iostream>
#include <cmath>
#include <vector>
#include <string>
using namespace std;
const int N = 51; // Size of the grid; two columns/rows are added at the beginning and the end of the array (no evolution of the CA on the boundaries)
class Cell{
//defining whats a cell here
};
void showCA(Cell CA[N+2][N+2]){
//function to print the CA grid in the terminal
}
Cell[N+2][N+2] evol(Cell CA[N+2][N+2]){
return CA;
}
int main()
{
// Initialisation
cout << "Initialisation" << endl;
static Cell CA[N+2][N+2];
// some code here to initialize properly the Cell array.
showCA(CA);
CA = evol(CA);
showCA(CA);
return 0;
}
编译器返回此错误:
error: expected unqualified-id
Cell[N+2][N+2] evol(Cell CA[N+2][N+2]){
关于我应该如何实现它的任何想法?
【问题讨论】:
-
使用分号来修复该错误。您可能也想使用向量。
-
N究竟代表什么?您的代码中有许多奇怪的语句。你最终想达到什么目标? -
创建一个类来表示一个多维数组(可能作为一个包装器来提供二维寻址到
std::vector)。返回该类的一个实例。 -
过去几十年的标准方法是提供一个目标数组作为 evol 的参数,它不会返回任何内容或可能返回 bool,表示成功。调用者可能会有两个数组,它们在每次进化后将角色交换为源和目标。
-
您声明返回数组的函数的语法不正确。使用
Cell evol(Cell CA[N+2][N+2])[N+2][N+2]而不是Cell[N+2][N+2] evol(Cell CA[N+2][N+2])可以获得更有用的错误消息。
标签: c++ function multidimensional-array