I want to...
1) make a simple class Object that stores some integers etc...
2) put this Object class into a vector
3) Loop through the vector and call the class member function stats() which prints info the Object is storing.
I have tested Object and it should work fine my problem is getting classes into the vector and then dereferencing the function stats. Any help greatly appreciated I don't think this should be that difficult, but I don't know the answer.
Code:#include<iostream> #include<vector> using namespace std; class Object { public: int mCurPosX; int mCurPosY; int mNewPosX; int mNewPosY; int mSymbol; //integer that will be cast to a character when printed static int sNumObjects; Object(int curPosX, int curPosY, int symbol); void stats(); }; int Object::sNumObjects = 0; Object::Object(int curPosX, int curPosY, int symbol) { mCurPosX = curPosX; mCurPosY = curPosY; mSymbol = symbol; mNewPosX = mCurPosX; mNewPosY = mCurPosY; ++sNumObjects; } void Object::stats() { cout << "=========== Object " << this << " Stats ===========\n"; cout << "mCurPosX = " << mCurPosX << endl; cout << "mCurPosY = " << mCurPosY << endl; cout << "mSymbol = " << mSymbol << "\n\n"; cout << "mNewPosX = " << mNewPosX << endl; cout << "mNewPosY = " << mNewPosY << "\n\n"; cout << "Total Number of Objects = "<< sNumObjects << endl; } //######################################### // PROBLEM AREA BELOW //######################################### int main() { vector<Object*> pVectorObj; for(int i=0; i<=10; ++i) { Object* temp = new Object(0,0,97); pVectorObj.push_back(temp); } for(vector<Object*>::const_iterator iter = pVectorObj.begin(); iter != pVectorObj.end(); ++iter) { cout << "iter = " << *iter << endl; //produces error iter->.stats(); //############# //error: stats not declared *iter.stats(); //error: stats not declared *(*iter).stats(); } cout << "pVectorObj.size() = " << pVectorObj.size() << endl; system("pause"); return 0; }



LinkBack URL
About LinkBacks


