By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,143 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,143 IT Pros & Developers. It's quick & easy.

forward declaration vs. typedef

P: n/a
After over a decade of programming in C++ I seem to have missed some
substantial point (mental note: am I getting too old for this?). A
little bit of help would be *very* appreciated.

I'm trying to gather various different classes into a common namespace
using typedefs:

class QWidget {};
class MyListview {};
namespace gui
{
typedef QWidget Widget;
typedef MyListview Listview;
}

Thus, I can refer to these classes as gui::Widget and gui::Listview
regardless of their actual origin. So far so good.

Unfortunately, I need to forward declare these classes in a header:

namespace gui { class Widget; }
namespace gui { class Listview; }

At some point, the forward declaration "meets" the typedef. This is
where the compiler disagrees: the forward declaration is in conflict
with the typedef.

error C2371: 'gui::Widget' : redefinition; different basic types, see
declaration of 'gui::Widget'
error C2371: 'gui::Listview' : redefinition; different basic types,
see declaration of 'gui::Listview'

Is the type defined using typedef not a class? How else should I
define the namespace or change the forward declaration?

Here is a complete example:

class QWidget {};
class MyListview {};

namespace gui { class Widget; }
namespace gui { class Listview; }

namespace gui
{
typedef QWidget Widget;
typedef MyListview Listview;
}

void main() {}

Please help an old man ;) Thanks,
Stoyan.

Btw, just in case this is relevant:
Microsoft (R) 32-bit C/C++ Standard Compiler Version 13.10.3077 for
80x86
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Plok Plokowitsch wrote:
After over a decade of programming in C++ I seem to have missed some
substantial point (mental note: am I getting too old for this?). A
little bit of help would be *very* appreciated.

I'm trying to gather various different classes into a common namespace
using typedefs:

class QWidget {};
class MyListview {};
namespace gui
{
typedef QWidget Widget;
typedef MyListview Listview;
}

Thus, I can refer to these classes as gui::Widget and gui::Listview
regardless of their actual origin. So far so good.

Unfortunately, I need to forward declare these classes in a header:

namespace gui { class Widget; }
namespace gui { class Listview; }

At some point, the forward declaration "meets" the typedef. This is
where the compiler disagrees: the forward declaration is in conflict
with the typedef.

error C2371: 'gui::Widget' : redefinition; different basic types, see
declaration of 'gui::Widget'
error C2371: 'gui::Listview' : redefinition; different basic types,
see declaration of 'gui::Listview'

Is the type defined using typedef not a class?
typedef introduces a new name for an existing type. A forward declaration
introduces a new type.
How else should I define the namespace or change the forward declaration?


Well, if it's a typedef, it cannot suddenly become a new, different type.
First forward declare your class, then typedef it:

class QWidget;
class MyListView;

namespace gui
{
typedef QWidget Widget;
typedef MyListview Listview;
}

Jul 23 '05 #2

P: n/a
On 2005-05-30, Plok Plokowitsch <pl*********@hotmail.com> wrote:
After over a decade of programming in C++ I seem to have missed some
substantial point (mental note: am I getting too old for this?). A
little bit of help would be *very* appreciated.

I'm trying to gather various different classes into a common namespace
using typedefs:

class QWidget {};
class MyListview {};
namespace gui
{
typedef QWidget Widget;
typedef MyListview Listview;
}

Thus, I can refer to these classes as gui::Widget and gui::Listview
regardless of their actual origin. So far so good.

Unfortunately, I need to forward declare these classes in a header:

namespace gui { class Widget; }
namespace gui { class Listview; }


That's not going to work, because Widget is a typedef, not a name.

But the code above was the correct way to declare these classes (forward
declaration + typedef) So why not do that ?

I'm going to guess that you don't want to do that because the whole point of
the typedefs in the first place is to avoid littering your code with explicit
references to QWidget, so you don't want to forward declare QWidget in your
code.

A solution that I'd recommend would be to make a separate header that contains
the necessary forward declarations (see iosfwd for a similar example in the
std library!):

// gui_fwd.h
class QWidget {};
class MyListview {};
namespace gui
{
typedef QWidget Widget;
typedef MyListview Listview;
}

// my_header_that_needs_forward_declarations.h
#include <gui_fwd.h>

Cheers,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.