【发布时间】:2012-07-20 22:02:23
【问题描述】:
我需要访问一个向量指针元素,我的动画结构有以下代码(这里简化,不必要的变量被切断):
struct framestruct {
int w,h;
};
struct animstruct {
vector<framestruct> *frames;
};
vector<framestruct> some_animation; // this will be initialized with some frames data elsewhere.
animstruct test; // in this struct we save the pointer to those frames.
void init_anim(){
test.frames = (vector<framestruct> *)&some_animation; // take pointer.
}
void test_anim(){
test.frames[0].w; // error C2039: 'w' : is not a member of 'std::vector<_Ty>'
}
该数组有效,我通过以下方式对其进行了测试:
test.frames->size() 和我计划的一样是 7 点。
那么如何从向量中访问第 N 个索引处的向量元素(w 和 h)?
【问题讨论】:
-
另外,你不需要在这里使用 C 风格的转换。写
test.frames = &some_animation; -
@qehgt,啊,我在想我做错了什么/矫枉过正。
-
如果有构造函数,为什么会有
init_anim?编写没有太多花里胡哨的纯 C++ 代码通常很好,但这似乎有点过头了。 -
@pmr,我只是让代码尽可能简单。 -> 别担心,我确实使用构造函数!
标签: c++ visual-c++ vector c++03