#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct Item{
string itemName;
double cost;
};
class User{
public:
User() : itemList(0) {};
User(string a, string b) : fN(a), lN(b) {};
void addItem(Item a) {itemList.push_back(a);}
vector<Item> getItems() const {return itemList;}
void printUserInfo();
private:
string fN;
string lN;
vector<Item> itemList;
};
class Convention{
public:
Convention() : users(0) {};
void addUser(User a) {users.push_back(a);}
vector<User> getUsers() const {return users;}
void printUsers();
private:
vector<User> users;
};
int main(){
Convention con;
User a("Greg", "H");
User b("Tom", "D");
User c("John", "Doe");
con.addUser(a);
con.addUser(b);
con.addUser(c);
//THIS IS WHERE THE PROBLEM IS.... THE ITEM XX doesn't get added to the user, no idea why. I'm trying to get the first user and add an item to him.
Item xx = {"Knife", 5.25};
con.getUsers().at(0).addItem(xx);
con.printUsers();
}
void User::printUserInfo(){
cout<<"Firstname: " << fN << endl;
cout<<"Lastname: " << lN << endl;
cout<<"ItemCount: " << itemList.size() << endl;
}
void Convention::printUsers(){
for(int i = 0; i<users.size(); i++){
users.at(i).printUserInfo();
cout<<endl;
}
}
This is the output.....
Firstname: Greg
Lastname: H
ItemCount: 0
Firstname: Tom
Lastname: D
ItemCount: 0
Firstname: John
Lastname: Doe
ItemCount: 0
Press any key to continue . . .
Any ideas guys, thank you. For some reason, can pointer solve this?