473,793 Members | 2,922 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me W>
41 class ObjectOptionMen uDescribeObject
42 {
43 public:
44 std::string operator () (const W& object)
45 {
46 std::ostringstr eam desc;
47 desc << object;
48 return desc.str();
49 }
50 };
51
52 template<typena me T, typename D = ObjectOptionMen uDescribeObject <T> >
53 class ObjectOptionMen u : public Gtk::OptionMenu
54 {
55 public:
56 typedef T object_type;
57 typedef D description_fun c;
58
59 typedef std::vector<T> menu_list;
60
61 ObjectOptionMen u(const menu_list& list):
62 m_desc(),
63 m_options()
64 {
65 menu_list::cons t_iterator cur;
66 for (cur = list.begin();
67 cur != list.end();
68 ++cur)
69 add_item(*cur);
70 }
[...]
172 description_fun c 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:
objectoptionmen u.h: In constructor `Gtkmm::ObjectO ptionMenu<T,
D>::ObjectOptio nMenu(const std::vector<T, std::allocator< _CharT> >&)':
objectoptionmen u.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 posreturnsdialo g.h:19,
from epicpos.cc:24:
objectoptionmen u.h: In constructor `Gtkmm::ObjectO ptionMenu<T,
D>::ObjectOptio nMenu(const std::vector<_Ro w, std::allocator< _CharT> >&)':
objectoptionmen u.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 3016
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<typena me W>
41 class ObjectOptionMen uDescribeObject
42 {
43 public:
44 std::string operator () (const W& object)
45 {
46 std::ostringstr eam desc;
47 desc << object;
48 return desc.str();
49 }
50 };
51
52 template<typena me T, typename D = ObjectOptionMen uDescribeObject <T> >
53 class ObjectOptionMen u : public Gtk::OptionMenu
54 {
55 public:
56 typedef T object_type;
57 typedef D description_fun c;
58
59 typedef std::vector<T> menu_list;
60
61 ObjectOptionMen u(const menu_list& list):
62 m_desc(),
63 m_options()
64 {
65 menu_list::cons t_iterator cur;
typename menu_list::cons t_iterator cur;
66 for (cur = list.begin();
67 cur != list.end();
68 ++cur)
69 add_item(*cur);
70 }
[...]
172 description_fun c 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:
objectoptionmen u.h: In constructor `Gtkmm::ObjectO ptionMenu<T,
D>::ObjectOptio nMenu(const std::vector<T, std::allocator< _CharT> >&)':
objectoptionmen u.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 posreturnsdialo g.h:19,
from epicpos.cc:24:
objectoptionmen u.h: In constructor `Gtkmm::ObjectO ptionMenu<T,
D>::ObjectOptio nMenu(const std::vector<_Ro w, std::allocator< _CharT> >&)':
objectoptionmen u.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******@comca st.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<typena me W>
41 class ObjectOptionMen uDescribeObject
42 {
43 public:
44 std::string operator () (const W& object)
45 {
46 std::ostringstr eam desc;
47 desc << object;
48 return desc.str();
49 }
50 };
51
52 template<typena me T, typename D = ObjectOptionMen uDescribeObject <T> >
53 class ObjectOptionMen u : public Gtk::OptionMenu
54 {
55 public:
56 typedef T object_type;
57 typedef D description_fun c;
58
59 typedef std::vector<T> menu_list;
60
61 ObjectOptionMen u(const menu_list& list):
62 m_desc(),
63 m_options()
64 {
65 menu_list::cons t_iterator cur;


typename menu_list::cons t_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::cons t_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
<${******@inval id.whinlatter.u klinux.net.inva lid> wrote:
Jeffrey Schwab <je******@comca st.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<typena me W>
41 class ObjectOptionMen uDescribeObject
42 {
43 public:
44 std::string operator () (const W& object)
45 {
46 std::ostringstr eam desc;
47 desc << object;
48 return desc.str();
49 }
50 };
51
52 template<typena me T, typename D = ObjectOptionMen uDescribeObject <T> >
53 class ObjectOptionMen u : public Gtk::OptionMenu
54 {
55 public:
56 typedef T object_type;
57 typedef D description_fun c;
58
59 typedef std::vector<T> menu_list;
60
61 ObjectOptionMen u(const menu_list& list):
62 m_desc(),
63 m_options()
64 {
65 menu_list::cons t_iterator cur;
typename menu_list::cons t_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

ObjectOptionMen u<int>::menu_li st::const_itera tor i;
I tried putting

typename menu_list::cons t_iterator;

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

typedef typename menu_list::cons t_iterator const_iterator;

and then you can use "const_iterator " instead of "typename
menu_list::cons t_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 <${******@inval id.whinlatter.u klinux.net.inva lid> wrote in message news:<87******* *****@wrynose.w hinlatter.uklin ux.net>...
[snip]
52 template<typena me T, typename D = ObjectOptionMen uDescribeObject <T> >
53 class ObjectOptionMen u : public Gtk::OptionMenu
54 {
55 public:
56 typedef T object_type;
57 typedef D description_fun c;
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********@hot mail.com> writes:
On Thu, 18 Dec 2003 16:51:34 +0000, Roger Leigh
<${******@inval id.whinlatter.u klinux.net.inva lid> wrote:
Jeffrey Schwab <je******@comca st.net> writes:

typename menu_list::cons t_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

ObjectOptionMen u<int>::menu_li st::const_itera tor i;


I see.
I tried putting

typename menu_list::cons t_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 ObjectOptionMen u<> 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: objectoptionmen u.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_O BJECTOPTIONMENU _H
#define GTKMM_RLEXTRA_O BJECTOPTIONMENU _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<typena me T>
class ObjectOptionMen uDescribeObject
{
public:
typedef T object_type;

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

template<typena me T>
class ObjectOptionMen uItem : public Gtk::MenuItem
{
public:
typedef T object_type;

ObjectOptionMen uItem(const object_type& object,
const Glib::ustring& label):
Gtk::MenuItem(l abel),
m_object(object )
{}

virtual ~ObjectOptionMe nuItem()
{}

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

private:
object_type m_object;
};

template<typena me T, typename D = ObjectOptionMen uDescribeObject <T> >
class ObjectOptionMen u : public Gtk::OptionMenu
{
public:
typedef T object_type;
typedef D description_fun c;

typedef std::vector<obj ect_type> menu_list;

ObjectOptionMen u():
describe_object (),
m_options(),
m_menu()
{
}

ObjectOptionMen u(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 ObjectOptionMen u(BaseObjectTyp e* cobject,
const Glib::RefPtr<Gn ome::Glade::Xml >& xml_interface):
describe_object (),
m_options(),
m_menu()
{}

virtual ~ObjectOptionMe nu()
{}

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

remove_menu();

for (typename menu_list::cons t_iterator cur = list.begin();
cur != list.end();
++cur)
{
m_options.push_ back(*cur);
Gtk::MenuItem *menu_item =
manage(new ObjectOptionMen uItem<object_ty pe>(*cur, describe_object (*cur)));
m_menu.append(* menu_item);
}

Gtk::OptionMenu ::set_menu(m_me nu);
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 ObjectOptionMen uItem<object_ty pe> *object_menu_it em =
dynamic_cast<co nst ObjectOptionMen uItem<object_ty pe>* >(menu_item_ptr );

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

return NULL;
}

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

const ObjectOptionMen uItem<object_ty pe> *object_menu_it em =
dynamic_cast<co nst ObjectOptionMen uItem<object_ty pe>* >(menu_item_ptr );

if (object_menu_it em != NULL)
{
if (object == object_menu_ite m->get_object() )
{
Gtk::OptionMenu ::set_history(c ur);
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_fun c describe_object ;
menu_list m_options;
Gtk::Menu m_menu;

}; // class OptionMenu

}; // namespace Gtkmm
#endif // GTKMM_RLEXTRA_O BJECTOPTIONMENU _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
2405
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 Collection<Animal>' msvc7: error C2039: 'Name' : is not a member of 'Collection<Traits>' But to me it seems pretty unambiguous, so I can't see why it's wrong. Could anybody give me a pointer, either to the standard or the basic
1
3975
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 size n. There are n!/(s!(n-s)!) ways to do this. Donald E. Knuth gives several methods (algorithms) to generate all the s-combinations in . In such procedure-oriented way, each s-combination is processed while it's being generated. In some
3
2961
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 following error messages: warning: implicit typename is deprecated, please see the documentation for details Can someone kindly suggest how to get rid of these warnings? The source code follows.
2
1592
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 that doesent use much memory. I know a lot of C++ programmers ,and they tolded me, that C++ templates are real solution for dynamical memory use programming.I readed 3 books about C++ , but I don't have a practice and a mass things about templates...
12
15647
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
5
2435
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 to his suggestion, I run into type inference problems when passing typedef templates as arguments to template functions. It seems the compiler (gcc 4.0) cannot resolve the typedef template with an instantiation of that typedef template. Below is...
4
2482
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 something like this: MyOtherClass<T> Type; but let's keep it simple for the moment */
5
3371
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 list. I started by using a linked list of types with templates like: struct MyClass1 {}; struct MyClass2 {}; struct MyClass3 {}; struct NullType {};
104
4624
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 feeling the answer I'm going to get back will be "no, because templates have taken on a life of their own since their original conception and now also affect compiler implementation" (read: not good, IMO. John
2
1713
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: http://www.ddj.com/cpp/184403850 My compiler (Visual Age C 6.0) rejects them. Are they supported? Were they ever introduced into the language standard? Here is an example:
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.