gpt4 book ai didi

c++ - 访问 vector 中的子结构值

转载 作者:行者123 更新时间:2023-11-28 05:12:18 25 4
gpt4 key购买 nike

<分区>

我有一些像这样的对象结构:

struct Object {
string type;
Color c;
float ambient, diffuse;
};

struct Sphere: Object {
Point center;
float radius;
};

struct Square: Object {
Point loc;
float len;
};

我有一个填充了 Sphere 和 Square 结构的 vector :

vector<Object> objs;
Sphere sp = //sphere stuff
Square sq = //square stuff
objs.push_back(sp);
objs.push_back(sq);

我可以很好地访问父结构中的值,但我无法弄清楚如何访问 Sphere 和 Square 结构中的值。这就是我现在正在做的事情:

cout << objs.at(i).type << endl; //This works
cout << objs.at(i).center.x << endl; //Not working

有人知道怎么做吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com