vector pushback compilation error

J

john isaac

this statement causes a compilation error:
memberlist.name.push_back(newname);


cannot convert std::string to char.

memberlist is a vector...i is an integer that's interating in a
loop...name is part of a struct Profile defined as string name.

Vector<Profile>memberlist (memberlist is a vector of type profile)

Struct Profile
{

string name;
};

newname is being passed in through a function parameter...

editName(vector<Profile>&memberlist, newname)


does anyone know why it does this??
 
P

PascalK

editName(vector<Profile>&memberlist, newname)
{
Profile *pNewProfile;
pNewProfile = new Profile;
pNewProfile->name = newname;
memberlist.push_back(pNewProfile);
}
 
B

BobR

john isaac wrote in message ...
this statement causes a compilation error:
memberlist.name.push_back(newname);

cannot convert std::string to char.


Yup! Try:

memberlist.name += newname;
or:
memberlist.at( i ).name += newname;
 
C

Chris Theis

john isaac said:
this statement causes a compilation error:
memberlist.name.push_back(newname);


cannot convert std::string to char.

memberlist is a vector...i is an integer that's interating in a
loop...name is part of a struct Profile defined as string name.

Vector<Profile>memberlist (memberlist is a vector of type profile)

Struct Profile
{

string name;
};

newname is being passed in through a function parameter...

editName(vector<Profile>&memberlist, newname)


does anyone know why it does this??


The reason is that the push_back method of the string class requires a
character and not a string. So you'll either have to push_back the
characters of the string that is to be appended one by one, or you'll head
for the append() method. This is actually the way I'd suggest ;-)

Cheers
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top