LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2010, 04:42 PM   #1
blaschi
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Rep: Reputation: 0
creating new list containers C++


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
 
Old 10-15-2010, 05:39 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by blaschi View Post
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.
 
Old 10-15-2010, 06:51 PM   #3
blaschi
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
In other words, I would have to make a list of objects.
This is what I have as an example:
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)
{
    listUser.add(User(name));
}

int main()
{
    User U;
    U.setName("Jane");
    return 0;
}
It's pretty evident that I'm doing wrong, I don't understand how I can add objects to a list.
Thank you.
 
Old 10-15-2010, 10:22 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
Quote:
Originally Posted by blaschi View Post
listUser.add(User(name));
Your User class doesn't have a public constructor that takes a string.
 
Old 10-15-2010, 10:36 PM   #5
blaschi
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 10-15-2010, 11:55 PM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
I think you misunderstood. You need to add the following method:

Code:
class User
{
...
public:
    User(const string &);
...
}

User::User(const string &name)
{
    this->setName(name);
}
Only then will the following line work:

Code:
listUser.add(User(name));
 
Old 10-16-2010, 01:41 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Assuming that you want to add existing users to the listUser attribute then code you have for setName is wrong:
Code:
void User::setName(string name)
{
    listUser.add(User(name));
}
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:

Code:
void User::setName(string name)
{
    this->name = name;
}
If you want to add a User object to the listUser attribute then rename the method to addFriend:

Code:
void User::addFriend(User * user)
{
    listUser.add(user);
}
and change the definition of listUser to:
Code:
List<User*> listUser;
In your constructor add a line:
Code:
listUser = new List<User*>();
However if you just want a list of names rather than string objects then addFriend() would be
Code:
void User::addFriend(String name)
{
    listUser.add(name);
}
 
Old 10-16-2010, 03:15 AM   #8
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Quote:
Originally Posted by blaschi View Post
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).
 
Old 10-16-2010, 11:10 AM   #9
blaschi
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
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!
 
Old 10-16-2010, 01:30 PM   #10
blaschi
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 10-16-2010, 08:10 PM   #11
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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).
 
Old 10-17-2010, 10:55 PM   #12
blaschi
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 10-18-2010, 12:44 AM   #13
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] creating a list of word using tr command arvindk.monu Programming 5 07-13-2010 12:02 AM
creating a mailing list muserob Linux - Newbie 1 02-07-2009 02:16 PM
how to free linked list containers? rgiggs Programming 3 07-30-2004 01:24 PM
Warning message when creating a scrolled list ? tceast Linux - General 2 12-31-2003 10:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration