0
#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'
}

I can't figure out why I'm getting the above compile error.

3
  • 1
    The error message is telling you that *(p_vs)[0] has type vector<a_struct>. Commented Oct 1, 2011 at 6:39
  • @Ray That shouldn't be the case. How does that follow from the code? Commented Oct 1, 2011 at 6:43
  • I gave an answer below. It is because of precedence. Commented Oct 1, 2011 at 6:46

1 Answer 1

2

In C++, [] and . have higher precedence than *.

Your last line

*(p_vs)[0].an_int=0; 

when fully parenthesized, is

*((p_vs[0]).an_int)=0; 

Since p_vs was declared as

vector <a_struct> * p_vs;

it is as if p_vs is an array of vector <a_struct> elements, so p_vs[0] is a vector<a_struct>.

And vector<a_struct> objects indeed do not have a member an_int.

Add some parens and you will get what you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I see. The * goes inside the parens. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.