Accessing a Class Variable Using A Void Pointer
-
Basically I want to loop through a link list that I have. It wouldnt be hard if in the link list their might be classes with different data types. So I am trying to use the void pointer. Because I dont know the data type of the class and I want to try and make a function that returns the address of the last class in the link list. I am able to use templates but everything works well until I try to get the void pointer in a class to equal the void pointer of the next class in the link list. I dont see why it doesnt work because I keep getting the error error C2227: left of '->NextObject' must point to class/struct/union. I am pointing to a class void pointer and Node is suppose to be a class so I dont understand why I am getting the error. I tried casting to fix this problem either I dont know how to do it well or it doesnt work to fix this.If anyone can help I would be extremly greatful. #include #include #include #include #include #include #include #include class Room { public: void *NextObject; Room(); }; Room::Room() { NextObject = 0; name = "blue"; } using namespace std; Room *RoomHead; template X *FindEndObject(X * Node) { void *Temp = Node; while (Temp!=0) { Temp = Temp->NextObject; cout << "MOo" << endl; } return Node; } void main() { void *NewRoom = FindEndObject(RoomHead); system("PAUSE"); }
-
Basically I want to loop through a link list that I have. It wouldnt be hard if in the link list their might be classes with different data types. So I am trying to use the void pointer. Because I dont know the data type of the class and I want to try and make a function that returns the address of the last class in the link list. I am able to use templates but everything works well until I try to get the void pointer in a class to equal the void pointer of the next class in the link list. I dont see why it doesnt work because I keep getting the error error C2227: left of '->NextObject' must point to class/struct/union. I am pointing to a class void pointer and Node is suppose to be a class so I dont understand why I am getting the error. I tried casting to fix this problem either I dont know how to do it well or it doesnt work to fix this.If anyone can help I would be extremly greatful. #include #include #include #include #include #include #include #include class Room { public: void *NextObject; Room(); }; Room::Room() { NextObject = 0; name = "blue"; } using namespace std; Room *RoomHead; template X *FindEndObject(X * Node) { void *Temp = Node; while (Temp!=0) { Temp = Temp->NextObject; cout << "MOo" << endl; } return Node; } void main() { void *NewRoom = FindEndObject(RoomHead); system("PAUSE"); }
A void* doesn't have a NextObject method. You would need to cast this to the appropriate object with something like Room* tmpRoom = reinterpret_cast(Node) tmpRoom will be null if the cast fails.
-
A void* doesn't have a NextObject method. You would need to cast this to the appropriate object with something like Room* tmpRoom = reinterpret_cast(Node) tmpRoom will be null if the cast fails.
Room* tmpRoom = reinterpret_cast(Node) Ummm...
static_cast
would suffice (and in general is preferrable toreinterpret_cast
.) tmpRoom will be null if the cast fails. Oh, this behavior is only provided bydynamic_cast
, and this type of cast can only be applied to convert from a virtual class to a virtual class, which is not the case. So, IMHOstatic_cast
should be used, and the cast does not fail ever (of course, ifNode
didn't point to an actualRoom
object, then the program could crash or whatever.) Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
A void* doesn't have a NextObject method. You would need to cast this to the appropriate object with something like Room* tmpRoom = reinterpret_cast(Node) tmpRoom will be null if the cast fails.
I tried doing so and with a few changes I finally got the program to compile with this modify function template void *FindEndObject(X *Node) { X* Temp1; if(Node->Next==NULL) { return Node->Next; } void *Temp = Node->Next; cout << "Hey Friend" << endl; while (Temp!=NULL) { cout << "Hey Friend" << endl; Temp1 = reinterpret_cast(Temp); Temp = Temp1->Next; } return Temp; } But not the problem is that their is a runtime error at if(Node->Next==NULL) I dont understand why their would be a error if I am trying to see if Next == Null;
-
I tried doing so and with a few changes I finally got the program to compile with this modify function template void *FindEndObject(X *Node) { X* Temp1; if(Node->Next==NULL) { return Node->Next; } void *Temp = Node->Next; cout << "Hey Friend" << endl; while (Temp!=NULL) { cout << "Hey Friend" << endl; Temp1 = reinterpret_cast(Temp); Temp = Temp1->Next; } return Temp; } But not the problem is that their is a runtime error at if(Node->Next==NULL) I dont understand why their would be a error if I am trying to see if Next == Null;
But not the problem is that their is a runtime error at if(Node->Next==NULL) I dont understand why their would be a error if I am trying to see if Next == Null; Maybe
Node
itself is null? Or maybe you're pasing an invalid pointer. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
But not the problem is that their is a runtime error at if(Node->Next==NULL) I dont understand why their would be a error if I am trying to see if Next == Null; Maybe
Node
itself is null? Or maybe you're pasing an invalid pointer. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloSorry I didnt see the post before I posted my last message and I tried to fix my program using the advice. I am still receiving the same runtime problem which stops at while (tmpRoom->Next!=0) Node shouldn't be NULL because Node would be a pointer to Room *RoomHead. Which is a global object. RoomHead->Next should = 0 because in the room constructure Next = 0; So FindEndObject should just return the value of tmpRoom because their is only one object in the link list. I tried every way to use the static_cast and I still cant get it to work. I am not even sure if I am doing it 100% correct. I dont want anyone to give me the code for the function because I will like to learn exactly how to do it my self. But if anyone could give me a little more detail hints I would appreciate it alot because right now I am not 100% sure how to implement the advice I am getting to my code. I guess because of the fact I never used void pointer or used any form of cast. This is how my program looks so far #include #include class Room { public: void *Next; Room(); }; Room::Room() { Next = 0; } using namespace std; Room *RoomHead; template X *FindEndObject(X * Node) { X* tmpRoom = Node; while (tmpRoom->Next!=0) { cout << "We Got Here" << endl; tmpRoom = static_cast(tmpRoom->Next); } return Node; } void main() { void *NewRoom = FindEndObject(RoomHead); system("PAUSE"); }
-
Sorry I didnt see the post before I posted my last message and I tried to fix my program using the advice. I am still receiving the same runtime problem which stops at while (tmpRoom->Next!=0) Node shouldn't be NULL because Node would be a pointer to Room *RoomHead. Which is a global object. RoomHead->Next should = 0 because in the room constructure Next = 0; So FindEndObject should just return the value of tmpRoom because their is only one object in the link list. I tried every way to use the static_cast and I still cant get it to work. I am not even sure if I am doing it 100% correct. I dont want anyone to give me the code for the function because I will like to learn exactly how to do it my self. But if anyone could give me a little more detail hints I would appreciate it alot because right now I am not 100% sure how to implement the advice I am getting to my code. I guess because of the fact I never used void pointer or used any form of cast. This is how my program looks so far #include #include class Room { public: void *Next; Room(); }; Room::Room() { Next = 0; } using namespace std; Room *RoomHead; template X *FindEndObject(X * Node) { X* tmpRoom = Node; while (tmpRoom->Next!=0) { cout << "We Got Here" << endl; tmpRoom = static_cast(tmpRoom->Next); } return Node; } void main() { void *NewRoom = FindEndObject(RoomHead); system("PAUSE"); }
-
Basically I want to loop through a link list that I have. It wouldnt be hard if in the link list their might be classes with different data types. So I am trying to use the void pointer. Because I dont know the data type of the class and I want to try and make a function that returns the address of the last class in the link list. I am able to use templates but everything works well until I try to get the void pointer in a class to equal the void pointer of the next class in the link list. I dont see why it doesnt work because I keep getting the error error C2227: left of '->NextObject' must point to class/struct/union. I am pointing to a class void pointer and Node is suppose to be a class so I dont understand why I am getting the error. I tried casting to fix this problem either I dont know how to do it well or it doesnt work to fix this.If anyone can help I would be extremly greatful. #include #include #include #include #include #include #include #include class Room { public: void *NextObject; Room(); }; Room::Room() { NextObject = 0; name = "blue"; } using namespace std; Room *RoomHead; template X *FindEndObject(X * Node) { void *Temp = Node; while (Temp!=0) { Temp = Temp->NextObject; cout << "MOo" << endl; } return Node; } void main() { void *NewRoom = FindEndObject(RoomHead); system("PAUSE"); }
-
RoomHead is a pointer not an object. As such it has never called the constructor. Declare it as Room RoomHead instead. Dave