可能重复:
声明在头文件中返回2D数组的函数?
我试图为2D数组提供一个简单的getter函数,我似乎无法找出发送它的正确语法.
目前,我有以下内容:
class Sample
{
public:
char **get2D();
private:
static const int x = 8;
static const int y = 10;
char two_d[x][y];
};
char** Sample::get2D()
{
return two_d;
};
Run Code Online (Sandbox Code Playgroud)
数组数组与指向数组的指针数组不同.在您的情况下,如果没有y在公共接口中发布数组()的宽度,则无法返回正确的类型.没有它,编译器不知道返回数组的每一行有多宽.
您可以尝试以下方法:
class Sample
{
public:
static const int x = 8;
static const int y = 10;
typedef char Row[y];
Row *get2D();
private:
char two_d[x][y];
};
Run Code Online (Sandbox Code Playgroud)