#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?

There is no pAVec member in your class, and neither was one passed to the member function test.

It doesn't work because the member function 'test' has no idea what pAVec is. pAVec is an instance of the object Member created in main. If you require a copy or reference of pAVec in test then pass it via a function parameter.

Thanks for the tip, the code now works, I have added

public:
vector<Member>*pAVec;

and now

this->SaveElementsInTheVector(pAVec,this);

works. Nice :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.