【发布时间】:2013-06-01 02:05:39
【问题描述】:
我不知道要搜索什么才能找到对此的解释,所以我在问。
我有这个报告错误的代码:
struct Settings{
int width;
int height;
} settings;
settings.width = 800; // 'settings' does not name a type error
settings.height = 600; // 'settings' does not name a type error
int main(){
cout << settings.width << " " << settings.height << endl;
但如果我将值赋值放在 main 中,它会起作用:
struct Settings{
int width;
int height;
} settings;
main () {
settings.width = 800; // no error
settings.height = 600; // no error
你能解释一下为什么吗?
编辑:
关于 Ralph Tandetzky 的回答,这是我的完整结构代码。你能告诉我如何像我的 sn-p 结构一样分配值吗?
struct Settings{
struct Dimensions{
int width;
int height;
} screen;
struct Build_menu:Dimensions{
int border_width;
} build_menu;
} settings;
【问题讨论】:
-
@jww 虽然你的观点是正确的,但我最喜欢Andy's answer here,因为它简单。也为有趣的标题变化点赞。
标签: c++ class struct variable-assignment