Vector of Class type compile issue

Hello,
I am fairly new to c++ and I am writing a program that should build a Person class where the data for each
person is stored in a vector with class type Emote.
The goal is to create a push_back member function to the Person class to build on itself. The problem I get is when I try to compile it - I get errors saying
the Emote class information is not defined.

"Error: no member named 'name' in
'std::vector<Person::Emote, std::allocator<Person::Emote> >'
Data.name=s1;"

Was wondering if anyone has run into this before and knows of a way to get the Emote member data 'available' to the rest of the class.

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
27
28
29
30
31
32
33
34
35
36
#include <vector>
#include <string>
#include <iostream>

class Person {

public:

void push_back(string s1, string s2, int val) {
	
	Data.push_back(Emote());
	
	Data.name=s1;
	Data.emotion=s2;
	Data.age=val;
}

private:

class Emote {	
public:
string name, emotion;
int age;
}; 
std::vector<Emote> Data;
};

int main() {
//does stuff

Person Group
     Group.push_back("Bob", "Angry", 32);

//do more stuff
return 0;
}


I think my understanding of how it works may just be wrong at this point. I think what I did was basically push_back onto the Data vector to open then assigned each part of the Emote a value. This may be the wrong way to do it though - any tips?

Summary - 2 Questions
1. Why is the Emote information not accessible to the rest of the Person class (i.e. what could the compiler be interpreting?)?
2. Is this the correct implementation for using push_back on the vector<class> Data? If not, how would i go about properly using the push_back function within the class?

Note: compiler is using c++11.

EDIT: added extra stuff to code (includes and main)

Any help is appreciated,
Thanks!
Last edited on
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <vector>
#include <string>

class Person {

    public:

        void push_back_a( std::string name, std::string emo, int age ) {

            Data.push_back( Emote() );
            
            // http://en.cppreference.com/w/cpp/container/vector/back
            Data.back().name = name ;
            Data.back().emotion = emo ;
            Data.back().age = age ;
        }

        // good: uniform initialisation
        // http://www.stroustrup.com/C++11FAQ.html#uniform-init
        void push_back_b( std::string name, std::string emo, int age ) {

            Data.push_back( { name, emo, age } ) ;
        }

        // better: construct in-place in the vector, copy construct the strings
        // http://en.cppreference.com/w/cpp/container/vector/emplace_back
        void push_back_c( std::string name, std::string emo, int age ) {

            Data.emplace_back( name, emo, age ) ;
        }

        // best: construct in-place in the vector, move construct the strings
        // http://en.cppreference.com/w/cpp/language/move_constructor
        // http://en.cppreference.com/w/cpp/utility/move
        void push_back_d( std::string name, std::string emo, int age ) {

            Data.emplace_back( std::move(name), std::move(emo), age ) ;
        }

    private:

        struct Emote { std::string name ; std::string emotion ; int age ; };

        std::vector<Emote> Data;
};
Very informative answer. Better yet, it compiled! I understand where I went wrong with syntax as well.

Thank you JLBorges! Appreciate the help with this!

I chose to go with the better approach as I am more familiar with the 'emplace_back()' member function.

Thanks again!
Topic archived. No new replies allowed.