Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer/iterator problem

Started by izua Dec 27, 2006 at 6:51 AM 5 replies 1.2k views
Original Post
izua
izua
I have two classes, a camera class, and a polygon manager class (holds items as vector of custom class "poly") One of the functions of the camera class requires an iteration of the polys inside the polygon manager class. I'm not sure exactly how I should do this. I believe that I have to pass somehow the instance of the polygon manager to the instance of the camera, but when it comes to using the iterator (and vector) on the pointer I should get, my lack of pointer skills definitely shows its face. Any ideas? thank you, izua [Edited by - izua on December 27, 2006 7:19:27 AM]
ToohrVyk
ToohrVyk
// If you need to alter the passed vector:void Camera::DoThings(std::vector<Polygon> & polygons) {  std::vector<Polygon>::iterator it;  for (it = polygons.begin(); it != polygons.end(); ++it) {    do_stuff_with_polygon(*it);  }}// otherwise, if it need not be changed:void Camera::DoOtherThings(const std::vector<Polygon> & polygons) {  std::vector<Polygon>::const_iterator it;  for (it = polygons.begin(); it != polygons.end(); ++it) {    do_other_stuff_with_polygon(*it);  }}

izua
izua
thanks
but if I want to pass the whole instance (of the polygon manager), and make changes on its vector, and its others variables, too?
Samsonite
Samsonite
Then pass the whole instance as a reference:

void Camera::DoStuff(PolygonManager& poly){    poly.x = 1; //example    poly.Polygons = ...    //and so on}
Hope I was helpful. And thank you if you were!
izua
izua
Well,

void CCamera::CheckColission(PolyMan& pm){    // Go through all the polygons    std::vector<Poly> pm.plist; //(plist is the iterator inside PolyMan)    std::vector<Poly>::iterator iter;    for (iter = pm.plist.begin(); iter != pm.plist.end(); ++iter) { //start iterating through polys    Cply=*iter;    .    .    .}


tells me

I"C:/Devcpp5/include/c++/3.4.2"  -I"C:/Devcpp5/include"    -fexpensive-optimizations -O3camera.cpp: In member function `void CCamera::CheckColission(PolyMan&)':camera.cpp:513: error: expected primary-expression before "pm"camera.cpp:513: error: expected `;' before "pm"


(line 513 beeing this one: std::vector pm.plist; (plist is the iterator inside PolyMan))

umm.. i don't understand what is wrong.
ToohrVyk
ToohrVyk
std::vector pm.plist; //(plist is the iterator inside PolyMan)

You don't need to declare/define it, it already exists. The line above should be removed.
izua
izua
done. now it crashes :/

[source="cpp"]void CCamera::CheckColission(PolyMan& pm){/ Go through all the polygons    std::vector<Poly>::iterator iter;    Poly Cply;    for (iter = pm.plist.begin(); iter != pm.plist.end(); ++iter) { //the real iteration    Cply=*iter;    // Store of the current poly(quad) we're testing    Point3D vQuad[4] = { Cply.corners[0], Cply.corners[1], Cply.corners[2], Cply.corners[3] };}


and somewhere in main, i'm doing

[source="cpp"]PolyMan polymanager;CCamera objcamera;//adding stuff in polymanager//checking keypresses to move objcamera accordinglyobjCamera.CheckColission(polymanager);


edit: Ok, fixed. it wasn't bugging with this. Seems to work fine. Thank you [smile]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.