8 Replies - 244 Views - Last Post: 08 February 2013 - 05:03 PM Rate Topic: -----

#1 caa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-January 13

Vector in the struct

Posted 08 February 2013 - 02:44 PM

Hi people,
here the following code I would like to run:
----------------------------------------------



struct students
{
std::string name;
std::vector<std::string>> friends;
}


int main
{

std::vectors<std::string> friendsdatabase; //this vector contains data imported from a csv file, 3 elements
// "John","Lucy", "bert"
struct student1;
student1.name="Anna"

//I would like to assign the elements of the vector friendsdatabase to the vector student1.friends in this way


 for (size_t m=0; m < friendsdatabase.size(); m++)
       {
        student1.friends.push_back(friendsdatabase[m]);
    }
// Here I get the the error "unable to resolve the identifier push_back
return 0;
}




Why the assigment does not work? How to assign a vector to a vector inside the struct?

Thanks for replies..

This post has been edited by jimblumberg: 08 February 2013 - 02:47 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: Vector in the struct

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2013
  • View blog
  • Posts: 3,038
  • Joined: 21-June 11

Re: Vector in the struct

Posted 08 February 2013 - 02:48 PM

When you write struct student1; declares a struct named student1, not a variable named student1. A variable needs a type and the struct keyword is not a type name. You probably meant students student1;.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Vector in the struct

Posted 08 February 2013 - 02:59 PM

There are so many things wrong with that snippet I suggest you start over. You are missing semicolons, parentheses, variable names, etc, etc.

Jim
Was This Post Helpful? 1
  • +
  • -

#4 caa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-January 13

Re: Vector in the struct

Posted 08 February 2013 - 03:13 PM

Yes I mean

struct students student1;

And you are right, we are missing one semicolon

student1.name="Anna";

But the fact ist it does not work....
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Vector in the struct

Posted 08 February 2013 - 03:20 PM

You are missing quite a few mandatory items, I suggest you start again. You have missing parentheses on line 8 for starters. You need to find the errors and fix them. There are too many for us to try to point out every one of them.

Jim
Was This Post Helpful? 1
  • +
  • -

#6 caa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-January 13

Re: Vector in the struct

Posted 08 February 2013 - 03:41 PM

Jim,
it is not the problem in parenthesis main()

The true problem is the vector assignment in the struct,
how to assign the new contents from friendsdatabase vector (it has been created and defined)
to the vector student1.friends...
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Vector in the struct

Posted 08 February 2013 - 03:55 PM

Well fix all the other errors and you may be able to find your problem. Then once you fix the basic errors repost your code.


Jim
Was This Post Helpful? 0
  • +
  • -

#8 caa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-January 13

Re: Vector in the struct

Posted 08 February 2013 - 04:23 PM

Jim a fixed code for you ...

#include <vector>
#include<string>


struct students
{
std::string name;
std::vector<std::string> friends;
};

int main()
{

std::vector<std::string> friendsdatabase; //this vector contains data imported from a csv file, 3 elements
// "John","Lucy", "Bert"

friendsdatabase.push_back("John");
friendsdatabase.push_back("Lucy");
friendsdatabase.push_back("Bert");


struct students student1;
student1.name="Anna";

//I would like to assign the elements of the vector friendsdatabase to the vector student1.friends in this way

 for (size_t m=0; m < friendsdatabase.size(); m++)
       {
        student1.friends.push_back(friendsdatabase[m]);
    }
// Here I get the the error "unable to resolve the identifier push_back


std::cout<<student1.friends[1]<<std::endl;
return 0;

}



It works pretty well in codepad.org..I think it is NetBeans problem..

This post has been edited by jimblumberg: 08 February 2013 - 05:00 PM
Reason for edit:: Added missing code tags. Please learn to use them properly.

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,749
  • Joined: 25-December 09

Re: Vector in the struct

Posted 08 February 2013 - 05:03 PM

So what seems to be your problem with your corrected code? After adding a missing header file it seems to run properly for me.

Quote

Jim a fixed code for you ...

By the way you didn't fix your code for me you fixed your code for yourself.

You also need to start using code tags when posting your code!

:code:



Jim

This post has been edited by jimblumberg: 08 February 2013 - 05:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1