Can't use function of object in pointer

Okay so basically I've got a vector, thePointerTest, that is holding pointers to pointerTest objects. What I need to know is how to call the print function for a printerTest object pointer while it is in the vector.

vector<pointerTest*> thePointerTest; ==> the vector

So how would I call the pointerTest.print(); function while the pointer is still in the vector?
What I have right now is

thePointerTest[count]->print();

While this compiles correctly, whenever I try to run the program it crashes after the line of code before this command, and before any information can be printed to the screen.

Any help would be greatly appreciated.

Last edited on
I'm guessing that you didn't actually insert anything into the vector?
Well here's the code where I put the pointerTest pointers into the vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector<pointerTest*> test2;

		for (int count2 = 0; count2 < 5; count2++)
		{
			pointerTest test3;
			pointerTest* test3Ptr;
			test3Ptr = &test3;

			cout << "Point 4." << count2 + 1 << endl;

			test3Ptr->setStuff(count2, count2+1, count2+2, count2+3);

			test2.push_back(test3Ptr);
		}


After checking I know that the pointerTest objects are being created I just can't figure out how to call a function for one from a pointer in a vector.
Last edited on
The pointers will be invalidated after each for loop is over (because the object they point to is destructed). Instead, you should allocate them with new (just don't forget to delete them).
Yeah I just realized that while looking at another post on here. It has now been changed to something like:

1
2
3
4
5
6
7
vector<pointerTest*> test2;

for( int count2 = 0; count2 < 5; count2++)
{
test2.push_back(new pointerTest)
test2[count]->setStuff(count2, count2+1, count2+2, count2+3);
}


Doing it this way is working perfectly.

Thanks for the help.
Topic archived. No new replies allowed.