acessing a vector within a structure

Hello, I'm having a problem adding values to a variable within a structure.
Here's a simplified version of my program(only with the part that isn't working)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

typedef struct dados
       {
       vector <string> nome;
       }dados;
    
vector <dados> contas;
main()
{

string temp;
cout<<"Qual o nome do Titular."<<endl;
cin.ignore(1,'n');
getline(cin, temp);
contas.nome.push_back(temp); //doesn't work here

cout << dados[0].nome[0] << endl; //doesn't work here

system("pause");
}


So, the idea is to have a vector of a structure(named 'contas'), and within this structure there's others vectors, that i want to access.

The error i get is:
22 E:\c++\teste2.cpp 'class std::vector<dados, std::allocator<dados> >' has no member named 'nome'

and
24 E:\c++\teste2.cpp expected primary-expression before '[' token

If you could give me some ideas on this..
thanks :)
On line 21 you are using a vector of dados, not a dados object. -The vector is empty there so you won't be able to use dados members until you don't add some objects to the vector-
On line 23 you are using the class name instead of the vector
line 21:
I don't understand what you mean, I'm trying to add "temp". I'm still new to this.
If i use the "vector <string> nome;" outside of the structure it works.
something like this:
1
2
3
4
5
6
vector<string>nome;
string temp;
cout<<"Qual o nome do Titular."<<endl;
getline(cin, temp);
nome.push_back(temp);
cout << nome[0];


line 23:
ahh you're right, silly me.
On line 21 you are trying to access "nome", but that is not a member of std::vector. I think you meant something like:

contas[0].nome = "blah"; //access the first element of contas, then get its name

However, as Bazzy said:
Bazzy wrote:
-The vector is empty there so you won't be able to use dados members until you add some objects to the vector-
ahh, i think I'm understanding what you're trying to say.
So something like this?
1
2
3
4
5
string temp;
cout<<"Qual o nome do Titular."<<endl;
getline(cin, temp);
contas[0].nome.push_back(temp);
cout << contas[0].nome[0] << endl;


This doesn't give me a compile error, but when i execute it, it terminates after i write something, probably accessing some invalid memory or something.
It appears that you are creating a vector of dados objects, which each contain a vector of strings. Is this what you need? Judging by the rest of the code, I wonder if one of the vectors is unnecessary.

Regardless, in nested container situations you have to add information from the outer-most container to the inner-most container. For example:
1
2
3
4
5
6
7
8
9
vector< vector<int> > v2;
//..
v2[0].pushback( 1 );                // error: v2 does not contain any elements (of type vector<int>)
//..
vector<int> temp;
temp.push_back( 1 );                // temp vector contains 1
v2.push_back( temp );               // now v2 contains a vector<int>, which contains 1.
//..
cout << v2.front().front() << endl; // outputs 1 

I found another way to do that i wanted.
Thanks anyway for your help!
Topic archived. No new replies allowed.