473,385 Members | 1,610 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,385 software developers and data experts.

Forward declaration problem

Hi,
I have 2 classes, h1 and h2, declared in 2 seperated header files h1.h
and h2.h and implementated in 2 seperated source files h1.c and h2.c.

The h1.h looks like this,

#ifndef H1_H
#define H1_H

#ifndef H2_H
#include "h2.h"
#endif

namespace ns {
class h1 {
<some stuff>
}
}
#endif
The h2.h looks like this,

#ifndef H2_H
#define H2_H

#include "h1.h"
// NOTICE that I include the header without condition

namespace ns {
class h2 {
h1 h1_obj;

<some stuff>
}
}
#endif

the h1.c looks like this,
#ifndef H1_H
#include "h1.h"
#endif

<some code>

when I compile h1.c using Visual C++ compiler I get an error, error
C2079: 'h1_obj' uses undefined class 'ns::h1'.

Then I updated h2.h by adding a forward declaration of class h1 like
this,
#ifndef H2_H
#define H2_H

#include "h1.h"

namespace ns {
class h1;

class h2 {
h1 h1_obj;

<some stuff>
}
}
#endif

But I get the same error.

Any idea?

Mar 17 '06 #1
1 1447

li*****@hotmail.com wrote:
Hi,
I have 2 classes, h1 and h2, declared in 2 seperated header files h1.h
and h2.h and implementated in 2 seperated source files h1.c and h2.c.

The h1.h looks like this,

#ifndef H1_H
#define H1_H

#ifndef H2_H
#include "h2.h"
#endif

namespace ns {
class h1 {
<some stuff>
}
}
#endif
The h2.h looks like this,

#ifndef H2_H
#define H2_H

#include "h1.h"
// NOTICE that I include the header without condition

namespace ns {
class h2 {
h1 h1_obj;

<some stuff>
}
}
#endif

the h1.c looks like this,
#ifndef H1_H
#include "h1.h"
#endif

<some code>

when I compile h1.c using Visual C++ compiler I get an error, error
C2079: 'h1_obj' uses undefined class 'ns::h1'.

Then I updated h2.h by adding a forward declaration of class h1 like
this,
#ifndef H2_H
#define H2_H

#include "h1.h"

namespace ns {
class h1;

class h2 {
h1 h1_obj;

<some stuff>
}
}
#endif

But I get the same error.

Any idea?


OK, solve the problem, I changed h1.h like this,

#ifndef H1_H
#define H1_H

namespace ns {
class h1 {
<some stuff>
}
}

#ifndef H2_H
#include "h2.h"
#endif

#endif

Now it is ok.

Mar 18 '06 #2

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

Similar topics

6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
11
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
2
by: Legendary Pansy | last post by:
Hello, I'm trying to accomplish the impossible by trying to do something equivalent of this example found here http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.as Starting with "Listing...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.