Thread: help linked list of objects

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    help linked list of objects

    Greetings from a newbie,
    I'm currently working on a project similar to facebook but way simpler using a command line interface. The user is able to create an account and he/she is entitled to add interests and create friendships with other users.
    The main algorithm that I developed is based upon an object which is the user and its attributes are the functions that add 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, add that user to the list and create a new list containing the user's personal info. In other words, create a list of objects.
    This is a sample of the algorithm I've thought of:

    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;
    }
    As you can see I get a compilation error that I don't understand: "no matching function for call to 'User::User()'

    Is someone willing to give me a hint? Just so I get on the right track.
    I would highly appreciate it. Thank you!

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    listUser.add(User(name));
    What is this? listUser is a List<string>, but you're trying to add a User. And User has no constructor that takes a string.

    Explain what you're trying to do with all this.
    Last edited by NeonBlack; 10-16-2010 at 12:38 PM.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    I just read this again, and I think I see what you're going for now. Take a look at this code:
    Code:
    class User
    {
    	private:
    	string name;
    	List<string> interests;
    	
    	public:
    	User(){}
    	~User(){}
    	void setName(string name);
    	void addInterest(string interest);
    };
    
    void User::setName(string name)
    {
    	this->name=name;
    }
    
    void User::addInterest(string interest)
    {
    	interests.Add(interest);
    }
    
    int main()
    {
    	User u;
    	u.setName("blaschi");
    	u.addInterest("C++");
    	
    	List<User> users;
    	users.add(u);
    	
    	return 0;
    }
    Now, notice that each user has two things: a name and a lit of interests, and we have a list of users that is external to the user class.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Ok, let me start over.
    I've managed to create a list of objects, as follows:
    Code:
    #include <iostream>
    #include <cstdio>
    #include <string>
    #include "List.h"
    
    using namespace std;
    template<class User> class List;
    
    class User
    {
        private:
        // a list inside the user object, containing all of its interests
        List<string> listInterest;
        string name;
        public:
        User() {}
        ~User() {}
    };
    
    int main()
    {
        List<User> listUser;
        listUser.add(User());
        return 0;
    }
    That works fine, and what I'm doing there is adding an object user to the main user list.
    Now this is what I'm trying to get right:
    I want to be able to identify the user object within the user list by its name, that way I know whether the user exists or not.
    So my question is: is it possible to add an object to the list identified by its name?
    like
    Code:
    listUser.add(User(name));
    I know that it won't work, I have to assign its name in the constructor, but as an example so I'm clear on what I'm trying to create.
    Thank you.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by blaschi
    is it possible to add an object to the list identified by its name?
    Not yet. But you could provide an appropriate constructor, e.g.,
    Code:
    explicit User(const std::string& name_) : name(name_) {}
    By the way, you can ditch the User destructor as the compiler generated version should suffice.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Thanks, I'll look into that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Linked List Class with Pointers to Objects and using Polymorphism
    By CaptainMorgan in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2006, 11:41 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM