#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Member
{
public:
string Name,Vorname;
//prototypes
vector<Member>*generateAVector();
void SaveElementsInTheVector(vector<Member>*pAVec,Member *obj);
void DisplayTheVector(vector<Member>*pTheVec);
void test();
};
int main()
{
Member Neu; //new object
//creates a vector on the heap, here with no sense,only experimentally
vector<Member>*pAVec=Neu.generateAVector();
//puts data in the elements
Neu.Name="Du";
Neu.Vorname="Er";
//Saving data in the vector
Neu.test(); //This doesn't work.
//Neu.SaveElementsInTheVector(pAVec,&Neu); //This works of course
//Displaying the vector on the monitor
Neu.DisplayTheVector(pAVec); //This works, too.
delete pAVec;
//pointer to zero for the case that someone will try get access to the pointer on the heap
pAVec=NULL;
return 0;
}
//Definition
//function for creating a vector on the heap
vector<Member>*Member::generateAVector()
{
return new vector<Member>(0);
}
//function for saving the vector
void Member::SaveElementsInTheVector(vector<Member>*pAVec,Member *obj)
{
pAVec->push_back(*obj);
}
//displaying the vector on the monitor
void Member::DisplayTheVector(vector<Member>*pTheVec)
{
vector<Member>::iterator pos;
for(pos=(*pTheVec).begin();pos!=(*pTheVec).end();++pos)
{
cout<<endl;
cout<<"Name: "<<pos->Name<<endl;
cout<<"Vorname: "<<pos->Vorname<<endl;
}
}
//print something on the monitor and starts the function "SaveElementsInTheVector()"
void Member::test()
{
cout<<"Now the vector ist going to be saved."<<endl;
//Shall call the function SaveElementsInTheVector()
this->SaveElementsInTheVector(pAVec,this);
}
I wrote some "nonsense"-code for testing this and that, for example in line 66, within the function "test()" there should be called another function"SaveElementsInTheVector()" it seems to be wrong, probably transferring of "pAVec" is wrong, but why? Maybe someone could help?