该声明在头文件中返回一个二维数组的函数? [英] Declaring a function that return a 2D array in a header file?
问题描述
我想声明,我的头文件中,返回一个二维数组的功能。这怎么可能完成,因为我们已经知道了数组的大小?下面是目前我在做什么。
类样品
{
上市:
CHAR [X] [Y] getArr();
无效等等(INT X,int y)对;
私人的:
量静态INT X = 8;
常量静态INT Y = 2;
ARR的char [X] [Y]
};
原来出我原来的答复是完全不正确的,但<一href=\"http://meta.stackexchange.com/questions/14932/allow-author-of-accepted-answer-to-delete-it-in-certain-circumstances\">I因为它被接受无法将其删除。从<一个href=\"http://stackoverflow.com/questions/4636144/declaring-a-function-that-return-a-2d-array-in-a-header-file/4636331#4636331\">two单独的<一个href=\"http://stackoverflow.com/questions/4636144/declaring-a-function-that-return-a-2d-array-in-a-header-file/4636361#4636361\">answers下面,我就能够编译如下:
类样品
{
量静态INT X = 8;
常量静态INT Y = 2;
上市:
焦炭的typedef SampleArray [X] [Y] SampleArray&安培; getArr();
无效等等(INT X,int y)对;私人的:
SampleArray ARR;
};样品:: SampleArray&安培;样品:: getArr()
{
返回ARR;
}
(我已整理我原来的解决方案只与OP的给定类的声明,而不是 getArr的定义()
)
就返回一个指针的指针。
的char ** getArr();
I am trying to declare, within my header file, a function that returns a 2D array. How can this be accomplished, given that we already know the size of the array? Below is what I'm currently doing.
class Sample
{
public:
char[x][y] getArr();
void blah(int x, int y);
private:
const static int x = 8;
const static int y = 2;
char arr[x][y];
};
It turns-out my original answer was totally incorrect, but I can't delete it since it's been accepted. From two separate answers below, I was able to compile this:
class Sample
{
const static int x = 8;
const static int y = 2;
public:
typedef char SampleArray[x][y];
SampleArray& getArr();
void blah(int x, int y);
private:
SampleArray arr;
};
Sample::SampleArray& Sample::getArr ()
{
return arr;
}
(I had compiled my original solution only with the OP's given class declaration, not the definition of getArr()
.)
Just return a pointer to a pointer.
char** getArr();
这篇关于该声明在头文件中返回一个二维数组的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!