Ji yong
Ji yong

Reputation: 433

ERROR: Expression Must have a pointer to class C++

Hi I faced this problem and I research and there should not be a problem with my code. So I not sure why this is happening, Basically I have this:

typedef struct {

Edge *next;
Edge *twin;

 } Edge;

Then I have a vector that stores this struct

std::vector<Edge*> EdgeList;

However when I try to access the member of this edge I face the above error.

Edge *e1
e1->next = EdgeList[index]->next->twin;

I am able access only up to one degree as in if I write my code in this way;

Edge *e1, *e2;
e2 = EdgeList[index]->next;
e1->next = e2->twin;

The error disappear. Note I have made sure that all the pointers are not NULL. I would like to ask why is it so? If I would to access several degrees, I cannot be declaring one variable at a time for each degree. Can someone help?

Upvotes: 0

Views: 110

Answers (1)

Borgleader
Borgleader

Reputation: 15916

Your problem is you've created an anonymous struct and a variable of that (anonymous) type called Edge, instead of creating a type called Edge.

Change the struct declaration to:

struct Edge
{
    Edge *next;
    Edge *twin;
};

P.S: You don't need to use typedef in C++.

P.P.S: I don't know what your use case is, but you should probably store the struct directly into the vector instead of pointers to them.

Upvotes: 2

Related Questions