c++ list

This is a discussion on c++ list within the C++ Programming forums, part of the General Programming Boards category; Hello.. I have a template class Code: template <class t> class datalist { public: datalist(string init) : name(init) { } ...

  1. #1
    l2u
    l2u is offline
    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,262
    vector< datalist<string> > or something? or list< datalist<int> > or whatever. You need to specify the template type

  3. #3
    l2u
    l2u is offline
    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,262
    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,020
    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
    l2u
    l2u is offline
    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,020
    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,262
    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,020
    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,317
    >> 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: template class // normal class

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, 05:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08: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, 04:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21