R
Roger Leigh
Although I've got over most of my template-related problems, I'm
having trouble when I started to use default template parameters.
For template type T, I've typedef'd this as object_type and then
typedef'd std::vector<T> menu_list. This doesn't seem to
work though:
40 template<typename W>
41 class ObjectOptionMenuDescribeObject
42 {
43 public:
44 std::string operator () (const W& object)
45 {
46 std:
stringstream desc;
47 desc << object;
48 return desc.str();
49 }
50 };
51
52 template<typename T, typename D = ObjectOptionMenuDescribeObject<T> >
53 class ObjectOptionMenu : public Gtk::OptionMenu
54 {
55 public:
56 typedef T object_type;
57 typedef D description_func;
58
59 typedef std::vector<T> menu_list;
60
61 ObjectOptionMenu(const menu_list& list):
62 m_desc(),
63 m_options()
64 {
65 menu_list::const_iterator cur;
66 for (cur = list.begin();
67 cur != list.end();
68 ++cur)
69 add_item(*cur);
70 }
[...]
172 description_func m_desc;
173 menu_list m_options;
174
175 }; // class OptionMenu
[Gtk::OptionMenu is a GUI menu widget.]
When I try to compile this (just included into an empty .cc file) I
get this:
$ g++ [lots of -I options] -c test.cc
In file included from test.cc:1:
objectoptionmenu.h: In constructor `Gtkmm::ObjectOptionMenu<T,
D>::ObjectOptionMenu(const std::vector<T, std::allocator<_CharT> >&)':
objectoptionmenu.h:65: error: syntax error before `;' token
It looks like menu_list isn't defined, which gives the parse error.
This doesn't change if I change menu_list to std::vector<T>, so I
think there may be something wrong with the template definition, but I
can't see what.
When I include the same header in a real source file with lots of
other headers included as well, the error is even stranger:
In file included from posreturnsdialog.h:19,
from epicpos.cc:24:
objectoptionmenu.h: In constructor `Gtkmm::ObjectOptionMenu<T,
D>::ObjectOptionMenu(const std::vector<_Row, std::allocator<_CharT> >&)':
objectoptionmenu.h:65: error: syntax error before `;' token
Where on earth have _Row and std::allocator<_CharT> appeared from?
[_Row is a template parameter in another header, but it's not
referenced at all.] How can another unrelated template parameter
"pollute" my template? (Especially since I've not yet instantiated
it.)
Possibly related: what is the scope of the template parameter name?
Does it have to be unique to just the enclosed class/function, or all
templates declared within that scope?
Many thanks,
Roger
having trouble when I started to use default template parameters.
For template type T, I've typedef'd this as object_type and then
typedef'd std::vector<T> menu_list. This doesn't seem to
work though:
40 template<typename W>
41 class ObjectOptionMenuDescribeObject
42 {
43 public:
44 std::string operator () (const W& object)
45 {
46 std:
47 desc << object;
48 return desc.str();
49 }
50 };
51
52 template<typename T, typename D = ObjectOptionMenuDescribeObject<T> >
53 class ObjectOptionMenu : public Gtk::OptionMenu
54 {
55 public:
56 typedef T object_type;
57 typedef D description_func;
58
59 typedef std::vector<T> menu_list;
60
61 ObjectOptionMenu(const menu_list& list):
62 m_desc(),
63 m_options()
64 {
65 menu_list::const_iterator cur;
66 for (cur = list.begin();
67 cur != list.end();
68 ++cur)
69 add_item(*cur);
70 }
[...]
172 description_func m_desc;
173 menu_list m_options;
174
175 }; // class OptionMenu
[Gtk::OptionMenu is a GUI menu widget.]
When I try to compile this (just included into an empty .cc file) I
get this:
$ g++ [lots of -I options] -c test.cc
In file included from test.cc:1:
objectoptionmenu.h: In constructor `Gtkmm::ObjectOptionMenu<T,
D>::ObjectOptionMenu(const std::vector<T, std::allocator<_CharT> >&)':
objectoptionmenu.h:65: error: syntax error before `;' token
It looks like menu_list isn't defined, which gives the parse error.
This doesn't change if I change menu_list to std::vector<T>, so I
think there may be something wrong with the template definition, but I
can't see what.
When I include the same header in a real source file with lots of
other headers included as well, the error is even stranger:
In file included from posreturnsdialog.h:19,
from epicpos.cc:24:
objectoptionmenu.h: In constructor `Gtkmm::ObjectOptionMenu<T,
D>::ObjectOptionMenu(const std::vector<_Row, std::allocator<_CharT> >&)':
objectoptionmenu.h:65: error: syntax error before `;' token
Where on earth have _Row and std::allocator<_CharT> appeared from?
[_Row is a template parameter in another header, but it's not
referenced at all.] How can another unrelated template parameter
"pollute" my template? (Especially since I've not yet instantiated
it.)
Possibly related: what is the scope of the template parameter name?
Does it have to be unique to just the enclosed class/function, or all
templates declared within that scope?
Many thanks,
Roger