开发者

Acessing a struct member, using a pointer to a vector of structs. Error:base operand of '->' has non-pointer type

开发者 https://www.devze.com 2023-02-04 01:07 出处:网络
#include <iostream> #include <vector> using namespace std; struct s_Astruct { vector <int> z;
#include <iostream>
#include <vector>

using namespace std;

struct s_Astruct {
    vector <int> z;
};


int main () 
{

    vector <s_Astruct> v_a;

    for(int q=0;q<10;q++)
    {
        v_a.push_back(s_Astruct());
        for(int w =0;w<5;w++)
            v_a[q].z.push_back(8);
    }

    vector <s_Astruct> * p_v_a = & v_a;
    cout << p_v_a[0]->z[4]; //error: base operand of '->' has non-pointer type 
                     //'__gnu_debug_def::vector<s_Astruct, std::allocator<s_Astruct> >'


}

There seems to be some issue with this sort of operation that I don't understand. In t开发者_运维知识库he code that I'm working on I actually have things like p_class->vector[]->vector[]->int; and I'm getting a similar error.


You want to do this:

cout << ((*p_v_a)[0]).z[4];

What you are doing is dereferencing the pointer by using [] and grabbing the 0'th offset, and then trying to dereference the non-pointer.

Another way to do it (which is just ugly):

cout << (p_v_a[0][0]).z[4];


p_v_a is a vector*, making p_v_a[0] a vector (since p_v_a[0] is equivalent to *(p_v_a+0)), so using the pointer to member operator (->) isn't going to work on that. You likely want (*p_v_a)[0].z[4].


When you use the index [] operator on the pointer, that also dereferences it. Using the infix dereference operator -> on the non-pointer type then results, correctly, in the error you describe.


In the following:

vector<s_Astruct>* p_v_a = &v_a;
cout << p_v_a[0]->z[4]

You have a pointer to a vector, but you didn't dereference that pointer before attempting to call operator[]. You then attempted to dereference the s_Astruct at index 0, but that is not a pointer.

Try this:

vector<s_Astruct>* p_v_a = &v_a;
cout << (*p_v_a)[0].z[4];

Hope that helps.


This was driving me buggo- I'd managed to get

std::cout << (nsNLs->back()).name

working, but I couldn't get the index array reference to work, (even with parens I didn't need).

std::cout << ||here?|| nsNLs ||here?|| [ intVal ] ||here?|| name

I tried every variation of dereferenceing and parenthesizing around the pointer to the vector, without hitting on ( ||thing pointed to by|| passedVariable )[ ].name

"." works because at the end of the two stage dereference, we're looking at an instance of the struct, not anything pointed to something, just a concrete chunk of memory.

@Mark, very clear explanation, you lead with something that works, and the operation is unusual enough that I feel better with the parens around the vector object dereference AND the array element / pointer dereference. I saw your solution, tried it, and it worked.

@Tomalek, Your answer is second easiest to follow and gets an extra point in my book for showing the problem, going through it step by step, explaining the error, and then suggesting the fix. Slightly sportier than Mark, only one set of parentheses.

@user470379, your answer is correct and you show a successful solution, but its bit harder to follow and the answer is at the end.

Thank you all three, I voted you up.

0

精彩评论

暂无评论...
验证码 换一张
取 消