473,326 Members | 2,805 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,326 software developers and data experts.

forward declaration vs. typedef

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
2 4461
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mjm | last post by:
Folks, Please help me with the following problems: ******************************************** 1. I have a class template template<class Base> class Matrix : public Base { /* .... */ }
2
by: jobseeker | last post by:
From: jobseeker95479@yahoo.com (jobseeker) Newsgroups: comp.lang.c++.moderated Subject: template and forward declaration NNTP-Posting-Host: 131.233.150.22 I have defined a class that contains a...
2
by: Jorge Yáñez | last post by:
Hello, It's possible to do a forward declaration of a typedef? Something similar to typedef TMyType; // typedef forward declaration void f ( TMyType& t )
5
by: Simon Elliott | last post by:
For some time I've been using typedefs for STL containers for reasons outlined in: http://www.gotw.ca/gotw/046.htm However a major downside to this is that you can't forward declare a typedef...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
2
by: Carlos Martinez Garcia | last post by:
Hi all: I usually make forward declarations in headers. Something like this: class MyClass; Now, I need a reference to a type defined like this (traditional C Style): typedef struct {
7
by: Noah Roberts | last post by:
I have something like the following: template<ENUM X, int I = 0> class Generic { .... }; typedef Generic<Val1> v1_type; typedef Generic<Val2> v2_type;
7
by: RedLars | last post by:
Need some help with a couple of questions. Previously I have seen this declaration; struct Student { int age; char name; }; and this definition of an object; struct Student stud;
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.