Jump to content
  • Advertisement
Sign in to follow this  
Basket Case

Vector of a Structure

This topic is 4774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to know how to create a vector of a structure. This is what I have so far, but it is not working...
struct address{
       string firstName;
       string lastName;
       string email;
       string phoneNumber;
       };

      Contacts.push_back(&address);//Compile error
      Contacts[0].firstName = str;
      Contacts[0].lastName = str;
      Contacts[0].email = str;
      Contacts[0].phoneNumber = str;
str being a string value from earlier.

Share this post


Link to post
Share on other sites
Advertisement
When you call push_back() you need to supply an address object to copy.


std::vector<address> Contacts;

Contacts.push_back(address());
Contacts[0].firstName = str;
Contacts[0].lastName = str;
Contacts[0].email = str;
Contacts[0].phoneNumber = str;

Share this post


Link to post
Share on other sites
Given that address is the name of a type, and not of a variable, your code reads a bit as if you had written Contacts.push_back(&int);, which you will agree doesn't mean much...

There are two approaches you can take. The first is to create a variable outside the vector and then push it in - you can either modify that original or later modify the one in the vector. The other solution is to just pass a temporary variable to push_back:

address contact;

contact.firstName = str;
contact.lastName = str;
contact.email = str;
contact.phoneNumber = str;

Contacts.push_back(contact);


or

address contact;

Contacts.push_back(contact);
Contacts.back().firstName = str;
Contacts.back().lastName = str;
Contacts.back().email = str;
Contacts.back().phoneNumber = str;


or

Contacts.push_back(address());
Contacts.back().firstName = str;
Contacts.back().lastName = str;
Contacts.back().email = str;
Contacts.back().phoneNumber = str;


The first and third solution are probably the most preferable.

Also note how I used Contacts.back() instead of Contacts[0]. Since you're using push_back, the new element gets added to the back of the vector. Contacts[0] always refer to the first element (as Contacts.front() would). If you have more than one element in the vector, that probably won't do what you want. [smile] Contacts.back() will always be the last element in the vector.

Share this post


Link to post
Share on other sites
On the other hand, it's not unreasonable to add a constructor to the struct (it will still qualify as a struct, and not require copy ctor or anything else complicated - since each piece of data is an actual object that already handles that stuff, treating the whole struct memberwise is ok):


struct address {
string firstName;
string lastName;
string email;
string phoneNumber;
address(string firstName, string lastName, string email, string phoneNumber)
: firstName(firstName), lastName(lastName), email(email), phoneNumber(phoneNumber) {}

};

vector<address> Contacts;

// Do it all in one breath, yet readably :)
Contacts.push_back(address(str, str, str, str));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!