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!