Problem with Dynamic Arrays
i made an arry of planes like this:
CPlane tplane[100];
now i want to invoke the contrstor on all of them so i can make the planes with my three points, but i dont know how, this is what im doing right now:
for(int x = 0;x < 100;x++){
tplane[x] = new CPlane(something1,something2,something3);
}
but not working giving me a compile error
When you declared the array, the default constructor was already called on every CPlane in it.
Given that you have an array of CPlane, and not an array of pointers to CPlane, it is an error to try to assign a CPlane pointer (returned by new) to an element of the array
Possibilities :
a) tplane[x] = CPlane( something1, something2, something3 ); ... watch out for copy constructor issues
b) CPlane* tplane[100]; ... but you''ll have to make sure to call delete on each of them.
c) std::vector<CPlane> tplane( 100, CPlane( something1, something2, something3 ) ); ... but now you have a STL vector, and not a C array (extract a pointer to the underlying array with &tplane[0]; )
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
Given that you have an array of CPlane, and not an array of pointers to CPlane, it is an error to try to assign a CPlane pointer (returned by new) to an element of the array
Possibilities :
a) tplane[x] = CPlane( something1, something2, something3 ); ... watch out for copy constructor issues
b) CPlane* tplane[100]; ... but you''ll have to make sure to call delete on each of them.
c) std::vector<CPlane> tplane( 100, CPlane( something1, something2, something3 ) ); ... but now you have a STL vector, and not a C array (extract a pointer to the underlying array with &tplane[0]; )
Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement