Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 14th, 2007, 10:00 PM   #1
l2u
Newbie
 
Join Date: Mar 2007
Posts: 24
Rep Power: 0 l2u is on a distinguished road
template specialization problem

Hello

I would like to do:

Code:

std::list<some_templated_class> _list;
_list.push_back(some_templated_class<some_object>(arguments));

but my compiler wont allow me to compile this. I get error:
unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type

Is there any other way to be able to do/override this?

Thanks for help
l2u is offline   Reply With Quote
Old Aug 14th, 2007, 10:47 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
I'm pretty sure you have to specify the template specialization of the class your list is holding when you declare it.

For example, instead of

std::list<Foo> _list;

You should write:
std::list<Foo<int> > _list;

That would be my guess anyway.
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!
Jessehk is offline   Reply With Quote
Old Aug 14th, 2007, 11:06 PM   #3
l2u
Newbie
 
Join Date: Mar 2007
Posts: 24
Rep Power: 0 l2u is on a distinguished road
Yeah but what if I dont know I want different types, not just int?
Maybe boost::any, boost::variant of anyone knows of better approach?
l2u is offline   Reply With Quote
Old Aug 15th, 2007, 12:34 AM   #4
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
The you should use a common base class like so:
cpp Syntax (Toggle Plain Text)
  1. #include <vector>
  2.  
  3. class Base
  4. {
  5. };
  6.  
  7. template<class T>
  8. class Templated : public Base
  9. {
  10. public:
  11. Templated(T val) : _val(val) {};
  12. T _val;
  13. };
  14.  
  15. int main()
  16. {
  17. // Note this works if you remove the pointers and the "new" statements
  18. // but I am not sure if you actually get at the derived object anymore
  19. std::vector<Base*> list;
  20. list.push_back(new Templated<int>(1));
  21. list.push_back(new Templated<double>(2.3));
  22.  
  23. return 0;
  24. }
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Aug 15th, 2007, 7:42 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
The answer to the original questions is that _list needs to be declared as;
std::list<some_templated_class<some_object> > _list;
If _list is a member of a templated class, it is necessary to insert the typename keyword; I'll leave working out where as an exercise.

Incidentally, it is not generally a good idea to have leading or trailing underscores on identifier names.
grumpy is offline   Reply With Quote
Old Aug 16th, 2007, 8:57 PM   #6
l2u
Newbie
 
Join Date: Mar 2007
Posts: 24
Rep Power: 0 l2u is on a distinguished road
Actually I would like to store handlers in containers (boost.bind), but I thought it would be the best to store them in new templated object.

I'd like to be able to do:

container.pushback(boost::bind(&Test::func,&test, someargument, argument2));
l2u is offline   Reply With Quote
Old Aug 17th, 2007, 2:27 AM   #7
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
You use boost.function of course.
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Template Problem pegasus001 C++ 13 Jan 18th, 2007 6:30 AM
friends, templates, and other s**t bl00dninja C++ 4 Oct 14th, 2006 2:15 AM
dev c++ software, template problem cairo C++ 11 Jun 2nd, 2006 1:42 PM
Template + operator problem Polyphemus_ C++ 3 Sep 30th, 2005 7:43 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC