为遇到返回多维数组问题的任何人更新
进一步阅读:Returning multidimensional array from function
我在标题中声明了一个 static int
变量。我已经在 .cpp
文件(实现文件?)中定义了它,如下面的相关代码所示...
卡片.h
#ifndef CARD_H
#define CARD_H
class Card {
private:
static int _palette[][3];
public:
static int palette();
};
#endif /* CARD_H */
卡片.cpp
int Card::_palette[][3]= {
{168, 0, 32},
{228, 92, 16},
{248, 216, 120},
{88, 216, 84},
{0, 120, 248},
{104, 68, 252},
{216, 0, 204},
{248, 120, 248}
};
static int palette(){
return _palette;
}
但是当我编译时,我得到这个错误:
..\source\src\Card.cpp: In function 'int palette()':
..\source\src\Card.cpp:42:9: error: '_palette' was not declared in this scope
return _palette;
难道我的访问器函数 palette()
不能获取私有(private)成员 _palette
的值吗?
最佳答案
您忘记了Card::
int (*Card::palette())[3]{
return _palette;
}
你不应该在方法定义中有static
。此外,当您应该返回一个 int
时,您试图返回一个 int[][]
。
将您的类(class)更改为:
class Card {
private:
static int _palette[][3];
public:
static int (*palette())[3];
};
关于c++ - 使用公共(public)成员函数访问私有(private)成员变量时出错 : Variable "was not declared in this scope",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35307092/