#include <iostream>
#include <vector>
using namespace std;
struct a_struct { int an_int; };
int main ()
{
vector <vector <a_struct> > the_vec;
vector <a_struct> * p_vs;
p_vs = & the_vec[0];
*(p_vs)[0].an_int=0; //error: 'class __gnu_debug_def::vector<a_struct,
//std::allocator<a_struct> >' has no member named 'an_int'
}
我无法弄清楚为什么我收到上述编译错误。
答案 0 :(得分:2)
在C ++中,[]
和.
的优先级高于*
。
你的最后一行
*(p_vs)[0].an_int=0;
完全括号后是
*((p_vs[0]).an_int)=0;
由于p_vs
被声明为
vector <a_struct> * p_vs;
好像p_vs
是一个vector <a_struct>
元素数组,因此p_vs[0]
是vector<a_struct>
。
vector<a_struct>
个对象确实没有成员an_int
。
添加一些parens,你会得到你想要的。