473,395 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Templates and typedef

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::ostringstream 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

--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
Jul 22 '05 #1
5 2983
Roger Leigh wrote:
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::ostringstream 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;
typename 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


Jul 22 '05 #2
Jeffrey Schwab <je******@comcast.net> writes:
Roger Leigh wrote:
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::ostringstream 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;


typename menu_list::const_iterator cur;


Thanks, that works just fine!

Is it possible to declare this as a type, rather than using typename
each time?
I tried putting

typename menu_list::const_iterator;

just after the typedefs, but that doesn't work. I'd rather the users
of the class didn't need to use typename--is there any way to achieve
this?
Thanks again,
Roger

--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
Jul 22 '05 #3
On Thu, 18 Dec 2003 16:51:34 +0000, Roger Leigh
<${******@invalid.whinlatter.uklinux.net.invalid > wrote:
Jeffrey Schwab <je******@comcast.net> writes:
Roger Leigh wrote:
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::ostringstream 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;
typename menu_list::const_iterator cur;


Thanks, that works just fine!

Is it possible to declare this as a type, rather than using typename
each time?


You only have to use typename when using a member of a dependent type
that is also a type. So it only applies to code inside a template
definition. e.g. this is fine

ObjectOptionMenu<int>::menu_list::const_iterator i;
I tried putting

typename menu_list::const_iterator;

just after the typedefs, but that doesn't work.
You could add

typedef typename menu_list::const_iterator const_iterator;

and then you can use "const_iterator" instead of "typename
menu_list::const_iterator".
I'd rather the usersof the class didn't need to use typename--is there any way to achieve
this?


The users of which class? How do they use it?

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4
Roger Leigh <${******@invalid.whinlatter.uklinux.net.invalid > wrote in message news:<87************@wrynose.whinlatter.uklinux.ne t>...
[snip]
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;

[snip]

I think you will like the new Gtk::ComboBox in gtkmm 2.4 which seems
to do what you want (any data types), with a model/view separation as
well.
Jul 22 '05 #5
tom_usenet <to********@hotmail.com> writes:
On Thu, 18 Dec 2003 16:51:34 +0000, Roger Leigh
<${******@invalid.whinlatter.uklinux.net.invalid > wrote:
Jeffrey Schwab <je******@comcast.net> writes:

typename menu_list::const_iterator cur;


Thanks, that works just fine!

Is it possible to declare this as a type, rather than using typename
each time?


You only have to use typename when using a member of a dependent type
that is also a type. So it only applies to code inside a template
definition. e.g. this is fine

ObjectOptionMenu<int>::menu_list::const_iterator i;


I see.
I tried putting

typename menu_list::const_iterator;

just after the typedefs, but that doesn't work. I'd rather the users of the class didn't need to use typename--is
there any way to achieve this?


The users of which class? How do they use it?


I wasn't aware of exactly how typename worked. This question is
wrong--I meant code using the ObjectOptionMenu<> class, but since they
don't need to use typename anyway, it's a non-issue.
For anyone else having problems with templates (and default
parameters), my working code follows:
// object-based option menu -*- C++ -*-
// $Id: objectoptionmenu.h,v 1.2 2003/12/14 16:14:54 roger Exp $
//
// Copyright (C) 2003 Roger Leigh.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
////////////////////////////////////////////////////////////////////////////

#ifndef GTKMM_RLEXTRA_OBJECTOPTIONMENU_H
#define GTKMM_RLEXTRA_OBJECTOPTIONMENU_H

#include <cassert>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

#include <gtkmm/optionmenu.h>
#include <gtkmm/menu.h>

#include <libglademm/xml.h>

namespace Gtkmm
{
template<typename T>
class ObjectOptionMenuDescribeObject
{
public:
typedef T object_type;

std::string operator () (const object_type& object) const
{
std::ostringstream desc;
desc << object;
return desc.str();
}
};

template<typename T>
class ObjectOptionMenuItem : public Gtk::MenuItem
{
public:
typedef T object_type;

ObjectOptionMenuItem(const object_type& object,
const Glib::ustring& label):
Gtk::MenuItem(label),
m_object(object)
{}

virtual ~ObjectOptionMenuItem()
{}

const object_type& get_object() const
{
return m_object;
}

private:
object_type m_object;
};

template<typename T, typename D = ObjectOptionMenuDescribeObject<T> >
class ObjectOptionMenu : public Gtk::OptionMenu
{
public:
typedef T object_type;
typedef D description_func;

typedef std::vector<object_type> menu_list;

ObjectOptionMenu():
describe_object(),
m_options(),
m_menu()
{
}

ObjectOptionMenu(const menu_list& list):
describe_object(),
m_options(),
m_menu()
{
add_menu(list);
}

/**
* Constructor for initialisation from a Glade interface description.
* @param cobject the GTK+ C object.
* @param xml_interface the Glade XML interface.
*/
explicit ObjectOptionMenu(BaseObjectType* cobject,
const Glib::RefPtr<Gnome::Glade::Xml>& xml_interface):
describe_object(),
m_options(),
m_menu()
{}

virtual ~ObjectOptionMenu()
{}

void set_menu(const menu_list& list)
{
std::cerr << "ObjectOptionMenu::add_menu()" << std::endl;

remove_menu();

for (typename menu_list::const_iterator cur = list.begin();
cur != list.end();
++cur)
{
m_options.push_back(*cur);
Gtk::MenuItem *menu_item =
manage(new ObjectOptionMenuItem<object_type>(*cur, describe_object(*cur)));
m_menu.append(*menu_item);
}

Gtk::OptionMenu::set_menu(m_menu);
Gtk::OptionMenu::set_history(0);
}

void remove_menu()
{
m_options.clear();
Gtk::OptionMenu::remove_menu();
m_menu.items().clear();
}

const object_type* get_history() const
{
int selected = Gtk::OptionMenu::get_history();

const Gtk::MenuItem& menu_item = get_menu()->items()[selected];

const Gtk::MenuItem* menu_item_ptr = &menu_item;

const ObjectOptionMenuItem<object_type> *object_menu_item =
dynamic_cast<const ObjectOptionMenuItem<object_type>* >(menu_item_ptr);

if (object_menu_item != NULL)
return &object_menu_item->get_object();

return NULL;
}

void set_history(const object_type& object)
{
Gtk::MenuShell::MenuList& menu_list = get_menu()->items();
Gtk::MenuShell::MenuList::size_type size = menu_list.size();
for (Gtk::MenuShell::MenuList::size_type cur = 0;
cur < size;
++cur)
{
const Gtk::MenuItem& menu_item = menu_list[cur];
const Gtk::MenuItem* menu_item_ptr = &menu_item;

const ObjectOptionMenuItem<object_type> *object_menu_item =
dynamic_cast<const ObjectOptionMenuItem<object_type>* >(menu_item_ptr);

if (object_menu_item != NULL)
{
if (object == object_menu_item->get_object())
{
Gtk::OptionMenu::set_history(cur);
return;
}
}
}
Gtk::OptionMenu::set_history(0);
}

void on_changed()
{
std::cerr << "OptionMenu::on_changed()" << std::endl;
std::cerr << " There are " << m_menu.items().size() << " items" << std::endl;
if (get_history() != NULL)
std::cerr << " Currently selected: " << describe_object(*get_history()) << std::endl;
else
std::cerr << " Nothing currently selected" << std::endl;
}

protected:
description_func describe_object;
menu_list m_options;
Gtk::Menu m_menu;

}; // class OptionMenu

}; // namespace Gtkmm
#endif // GTKMM_RLEXTRA_OBJECTOPTIONMENU_H
--
Roger Leigh

Printing on GNU/Linux? http://gimp-print.sourceforge.net/
GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Anthony Heading | last post by:
Hi all, I've often found myself wanting to write code like the example here. Since both MSVC and gcc both reject it, I suspect it is indeed illegal. gcc: no type named `Name' in `class...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
3
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the...
2
by: Bore Biko | last post by:
Dear, I am an ordinary C programmer and I am most interesed about dynamical data structuring and programming, I don't like to use matricess and rows, I like to program with practical programs...
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
5
by: jimmy | last post by:
I am trying to simulate typedef template similar to the suggestion of Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm However when implementing typedef templates according...
4
by: Sacha | last post by:
I'm aware, that up to date, "typedef templates" are not defined within the C++ standard. The seemingly common workaround is this: template <class T> struct MyTypeDef { /* ultimately I need...
5
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
2
by: tropos | last post by:
I'm trying to use the 'typedef templates' idiom, mentioned in Alexandrescu's work on templates. Here's a Dr Dobbs article from 2002 that says they are a proposed C++ standard:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.