ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I've got a quick question regarding list containers in c++.
I need to create a program where the user creates an account and he/she is entitled to add interests and creating friendships with other users.
The main algorithm depends on an object which is the user and its attributes are adding interests, friends, so on.
Users are stored in a linked list while their interests are stored in another list.
What I want to do is every time a new user is created dynamically create a new list during execution for personal info. storage. Is that possible?
Thanks in advance.
blaschi
What I want to do is every time a new user is created dynamically create a new list during execution for personal info. storage. Is that possible?
You just put the interest list as a member of the user object. That way, every time you add a user object to the user list, you will get a new interest list inside the user object. Inside the STL list it is using new to dynamically create the object, but that is neatly hidden away.
Last edited by neonsignal; 10-15-2010 at 05:44 PM.
Thanks but still no go.
I get the following compilation error: "no matching function for call to 'User::User()'
I don't think it makes much sense what I'm doing.
First the name of this method would imply that it is setting the name of the current user to name, rather than adding an object to the list of users. If that is what you want to do then the code would be:
In other words, I would have to make a list of objects.
This is what I have as an example:
...
I don't understand how I can add objects to a list.
I think you may be unclear about responsibilities here. It is the responsibility of the list of users to add a new user. It is the responsibility of the user to take care of user information (such as name and list of attributes).
So you have to think about how to structure this. My understanding is that you want a list of users, with each user having a list of attributes. So the User class should contain only a list of attributes, not the main list of users (it might have a list of friend users, but you should make that the second stage of your implementation, so that it doesn't obscure the analysis of the main classes).
So for example, if you were using STL lists:
Code:
#include <string>
#include <list>
// single user
class User
{
public:
User() {}
private:
std::string name;
std::list<std::string> interestList;
};
int main()
{
// list of users
std::list<User> userList;
// add a user to the list
userList.push_back(User());
}
I have left out the member functions to update the user name and attributes so that you can see the structure, but obviously you would need to add these too (graemef has described how to set up a friend list as well).
Great!! I think I got it now, I just had to define the list of Users in the main function.
I will implement the rest and keep you updated.
Thanks a lot!
I got the basic implementation down. Now what I want to do is identify each user within the list by its name.
For instance, I want to search for a particular user within the list by the user's name, that way I can verify if the user exists so the user can add interests:
Code:
#include <iostream>
#include <cstdio>
#include <string>
#include "List.h"
using namespace std;
template<class User> class List;
class User
{
private:
string name;
List<string> listInterest;
public:
User() {}
List<string> listUser;
void setName(string name);
~User() {}
};
void User::setName(string name)
{
this->name = name;
}
int main()
{
List<User> userList;
userList.add(User());
User U;
U.setName("Jane");
userList.search(User(name));
return 0;
}
Does the list have access to the name of the User? Or is the user stored just as an abstract entity? Thank you.
It's not possible to answer your question without seeing the List class.
However, some general suggestions. A Search for a specific User object may require a comparison method from the User class. This method would then perform a comparison on the User name. The Map data structure will index the collection on any value such as the name of the User then the object can be accessed as you were requesting.
Your code in the main section doesn't look correct. You are creating three User objects, an uninitialised one that goes on the list, one that is referenced by the variable U and one that uses a non-existent variable name and a non-existent constructor User(name).
Sorry for rising up this topic again, I just can't see a way around it.
I managed to add objects to the list and create another list within the user class, but what I just don't understand is how I can search for a User by its name.
Code:
using namespace std;
template<class User> class List;
class User
{
private:
string name;
public:
User() {}
~User() {}
void setName(string name);
void setInterest(string interest);
};
void User::setName(string name)
{
this->name=name;
}
void setInterest(string interest);
{
listInterest.add(interest);
//Returns the user's interest
cout << listInterest.find(interest).get();
}
int main()
{
User A;
//Set User's name
A.setName("John");
//Set user interest
A.setInterest("Play the piano");
//Add user to list
List<User> listUser;
listUser.add(A);
}
That works, but now my problem is that I don't know how I can search for a User within that list by its name. For instance the function listInterest.find(interest).get() returns the user's interest since interest is a string, but I can't do it with the user class because it's a clase. Will iterators solve my problem? Or pointers? Once I search for the user I need to compare its name to check if it exists.
Otherwise the user will not be created in the system since it already exists.
Thank you all.
You will need a search function. Pass to this function the list and the name you are looking for. Step through each node in the list comparing the name of each User object with the name passed in if you get a match return the User object.
Note: I'd suggest that you will want to pass the address of the list rather than the whole list otherwise you will be doing a lot of copying. Also you will want to return the address of the matched User rather than the User object itself, so that if no match is found you can return a null pointer.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.