#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?

Before we can help you, you'll have to clarify what the program's intended behavior is and identify what the problem is compared to the intended behavior.

Without any of that information, we have nothing to go on.

It looks like you have a lot of implied declarations, which you are not allowed to do. You should be getting warnings from your compiler about that.

There is a comment inside the main method.

INSTEAD OF THIS.....

Firstname: Greg
Lastname: H
ItemCount: 0

Firstname: Tom
Lastname: D
ItemCount: 0

Firstname: John
Lastname: Doe
ItemCount: 0

IT SHOULD SAY THIS

Firstname: Greg
Lastname: H
ItemCount: 1

Firstname: Tom
Lastname: D
ItemCount: 0

Firstname: John
Lastname: Doe
ItemCount: 0

con.getusers() returns a copy of the vector. You are then working on the copy of the vector; the vector inside con never gets changed.

I suspected that also. Can you please tell me what I must do instead? Like if you can please post the code example. I feel like I should be using call by reference instead of call by value somewhere.....

Before we can help you, you'll have to clarify what the program's intended behavior is and identify what the problem is compared to the intended behavior.

Without any of that information, we have nothing to go on.

It looks like you have a lot of implied declarations, which you are not allowed to do. You should be getting warnings from your compiler about that.

Compiler is not giving any warnings at all.

Compiler is not giving any warnings at all.

Oh, you're right. I read a section of your code wrong.

You are correct that you need to use either some pointers or references. I think it would be a good idea for you to try writing an operator[] for your Convention class. Said operator function would return a reference to the appropriate element of your user vector. Just make sure you verify that the requested index is within the appropriate range, otherwise, your program will have some serious stability issues.

At line 18 replace

vector<Item> getItems() const {return itemList;}

with

vector<Item>& getItems() {return itemList;}
commented: Good catch +1

At line 18 replace
vector<Item> getItems() const {return itemList;}
with
vector<Item>& getItems() {return itemList;}

This one did not work. Is there a way to do it with a pointer?

I see the problem at line 33 of your first post.
Replace that line with

vector<User> &getUsers() {return users;}

The problem is that you are getting a copy of the user then you are modifying that but not the original copy of it.

commented: Good catch +1

I see the problem at line 33 of your first post.
Replace that line with

vector<User> &getUsers() {return users;}

The problem is that you are getting a copy of the user then you are modifying that but not the original copy of it.

Thank you bro, it worked. I also tried this and it worked, but is it efficient?
Still I want to ask, was there a way to do this "somehow" with pointers?

#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() {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() {return users;}
	void setUsers(vector<User> usrs) {users = usrs;}
	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);


	Item xx = {"Knife", 5.25};

	//This works also
	vector<User> list = con.getUsers();
	list.at(0).addItem(xx);
	con.setUsers(list);
	

	



	
	

}

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;
	}
	

}

Thank you all for taking your time in helping me solve this problem.

If you wanted to make it work with a pointer instead, you could simply return a pointer to the vector you wanted to change from the function getUsers

Also, what would the difference be if I created a pointer type vector<User> *users in the Convention class instead of a regular one like vector<User> users. I'm like 1 week old on pointers and don't know very much about it. The only benefit I saw of using pointers for now was to create dynamic 1D/2D arrays and returning a pointer to an array.

Your constructor(s), destructor(s), and various operators will get substantially more complex to account for the dynamic allocation and de-allocation that will be required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.