我用新的“game_struct”声明了一个结构。它有以下内容:
struct game_struct{
scene scene_container[10];
player cPlayer;;
scene scene_one;
this->scene_container[0] = scene_one;
this->scene_container[0].image = " ";
this->scene_container[0].scene_message = "Welcome to the home screen of the game.";
};
它给出错误“此''之前的预期不合格ID”。我一直试图解决它,但我无法弄明白。任何建议将不胜感激。
答案 0 :(得分:3)
this->scene_container[0] = scene_one;
this->scene_container[0].image = " ";
this->scene_container[0].scene_message = "Welcome to the home screen of the game.";
您不能在this
game_struct
你想做的可能是:
struct game_struct{
scene scene_container[10];
player cPlayer;
scene scene_one;
game_struct(){
this->scene_container[0] = scene_one;
this->scene_container[0].image = " ";
this->scene_container[0].scene_message = "Welcome to the home screen of the game.";
}
};
答案 1 :(得分:-1)
上述解决方案scene_container[0]
和scene_one
不会引用相同的场景。 scene_container
只是从scene_one
分配的副本,这是毫无意义的,因为它们都是在构造game_struct
时默认构建的。这要好得多:
struct game_struct {
scene scene_container[10];
player cPlayer;
scene* scene_one;
game_struct() {
scene_one = scene_container; // points to first element
scene_one->image = " ";
scene_one->scene_message =
"Welcome to the home screen of the game.";
}
};
编辑:我希望我不必拼出来。但似乎这是我的第一个否定,我想我必须:
#include <string>
#include <iostream>
using namespace std;
struct scene {
string image;
string scene_message;
};
struct player {};
struct game_struct {
scene scene_container[10];
player cPlayer;
scene scene_one;
game_struct() /* all members are default constructed PRIOR TO ENTERING constructor block */
{
this->scene_container[0] = scene_one; /* this is not copy constructing, this is
COPY ASSIGNING two identically constructed
scene objects. All it does it non static member
copy assignment for every member of scene_container[0] */
this->scene_container[0].image = " ";
this->scene_container[0].scene_message =
"Welcome to the home screen of the game.";
}
};
int main(int argc, char** argv) {
game_struct gs;
cout << "scene_one.scene_message : " << gs.scene_one.scene_message << endl;
cout << "scene_container[0].scene_message : " << gs.scene_container[0].scene_message << endl;
}
这不足为奇:
scene_one.scene_message :
scene_container[0].scene_message : Welcome to the home screen of the game.