forward declaration vs. typedef

P

Plok Plokowitsch

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
 
R

Rolf Magnus

Plok said:
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;
}
 
D

Donovan Rebbechi

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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top