Thread: c++ list

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    c++ list

    Hello..

    I have a template class

    Code:
    template <class t>
    class datalist {
    public:
    	datalist(string init) : name(init) { }
    	string name;
    	vector <t> data;
    
    	void add(t &d);
    };
    How should I make c++ datalist linked list?

    I get error with:
    list <datalist> settings;

    Thanks for help

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    vector< datalist<string> > or something? or list< datalist<int> > or whatever. You need to specify the template type

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    I know, but i dont know whats the right way to do it... Its the problem that I want to link template classes with type string or int, so im really confused.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    do you mean datalist< vector<string> >, or datalist< list<string> > (where string could be anything, like int double float char* string etc)?

    >> template <class t>
    To avoid possible confusion, I think changing "class" to "typename" is a good idea.


    Could you expand on what it is you're trying to do, cause I'm slightly confused.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It's like twomers said.

    You need to do list< datalist<int> > or list< datalist<string> >.

    list <datalist>, because datalist is a template, it needs a template paramitter.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    I want to have a linked list of template class, where data in each class will be vector of strings or integrals.. So if I make new template class with string vector, or integral vector, I want to be able to insert BOTH in this list..

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You can't do that.

    Templates paramitters must be given compiletime, not runtime.

    What you can do is make datalist a regular class, and pass the string or number that you inisialize it with as a constructor arguement. Call the string constructor if you want a string class and the integer constructor if you want an int. Use a boolian value to remmember if the create datalist object is a string or int. Use a union inside the class for the string and int vectors, because the datalist cannot be both a string and int datalist at the same time.
    Last edited by King Mir; 09-04-2006 at 11:07 AM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Quote Originally Posted by l2u
    I want to have a linked list of template class, where data in each class will be vector of strings or integrals.. So if I make new template class with string vector, or integral vector, I want to be able to insert BOTH in this list..
    You could make three vectors (one for each possibiliey), and allow the user to choose from a menu ...

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You might also consider having a int_datalist and string_datalist as seperate polymorphic objects of the vertual datalist class.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I want to be able to insert BOTH in this list.
    Nothing has changed since the last time this question was answered. See the responses in this thread: http://cboard.cprogramming.com/showthread.php?t=82464

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM