Cyclic dependencies and namespaces.

C

Craig Sanders

Hello. I am hoping that someone can help me out as I can't seem to see what
I am doing wrong. I have a basic test program that is comprised of 5 files
called main.cc, classA.h and classA.cc, and classB.h and classB.cc. When I
attempt to compile this basic test program using version 3.3.3 of the GNU
g++ compiler under Fedora Core 2 Linux, it gives me the following error ;

Making all in src

make[1]: Entering directory
`/home/craig/source_code/c++/build/cyclic_namespaces/src'

g++ -DPACKAGE_NAME=\"cyclicNamespaces\" -DPACKAGE_TARNAME=\"cyclicNamespaces\"
-DPACKAGE_VERSION=\"0.0.1\" -DPACKAGE_STRING=\"cyclicNamespaces\
0.0.1\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"cyclicNamespaces\" -DVERSION=\"0.0.1\"
-DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1
-DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1
-DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -I. -I/home/craig/source_code/c++/src/cyclic_namespaces/src
-DHAVE_CONFIG_H -I/home/craig/source_code/c++/src/cyclic_namespaces/include
-g -O2 -MT main-main.o -MD -MP -MF .deps/main-main.Tpo -c -o main-main.o
`test -f 'main.cc' || echo
'/home/craig/source_code/c++/src/cyclic_namespaces/src/'`main.cc

In file included from
/home/craig/source_code/c++/src/cyclic_namespaces/include/classA.h:4,

from /home/craig/source_code/c++/src/cyclic_namespaces/src/main.cc:1:

/home/craig/source_code/c++/src/cyclic_namespaces/include/classB.h:14:
error: `

ns1' was not declared in this scope

/home/craig/source_code/c++/src/cyclic_namespaces/include/classB.h:14:
error: syntax

error before `::' token

make[1]: *** [main-main.o] Error 1

make[1]: Leaving directory
`/home/craig/source_code/c++/build/cyclic_namespaces/src'

make: *** [all-recursive] Error 1

The 2 offending files seem to be classA.h and classB.h which are implemented
as follows.

File : classA.h

===============

#ifndef CLASSA_H

#define CLASSA_H

#include <classB.h>

namespace ns1

{

namespace ns2

{

class ClassA

{

public :

int analyseObject(ns3::ns4::ClassB objectB);

};

}

}

#endif

File : classB.h

===============

#ifndef CLASSB_H

#define CLASSB_H

#include <classA.h>

namespace ns3

{

namespace ns4

{

class ClassB

{

public :

int analyseObject(ns1::ns2::ClassA objectA);

};

}

}

#endif

However, if I alter the file classB.h to look as follows, i.e. I comment out
the #include directive and the definition of the class's one and only
function/method ;

File : classB.h

===============

#ifndef CLASSB_H

#define CLASSB_H

// #include <classA.h>

namespace ns3

{

namespace ns4

{

class ClassB

{

public :

// int analyseObject(ns1::ns2::ClassA objectA);

};

}

}

#endif

and re-compile the basic program, then it compiles successfully and doesn't
complain. Why is this so and what do I need to do in order to get the
program to compile without resorting to commenting out the 2 lines of code?

Thankyou for any help that you can provide.
 
G

Glen Dayton

Craig said:
Hello. I am hoping that someone can help me out as I can't seem to see what
I am doing wrong. I have a basic test program that is comprised of 5 files
called main.cc, classA.h and classA.cc, and classB.h and classB.cc. When I
attempt to compile this basic test program using version 3.3.3 of the GNU
g++ compiler under Fedora Core 2 Linux, it gives me the following error ;

Making all in src

make[1]: Entering directory
`/home/craig/source_code/c++/build/cyclic_namespaces/src'

g++ -DPACKAGE_NAME=\"cyclicNamespaces\" -DPACKAGE_TARNAME=\"cyclicNamespaces\"
-DPACKAGE_VERSION=\"0.0.1\" -DPACKAGE_STRING=\"cyclicNamespaces\
0.0.1\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"cyclicNamespaces\" -DVERSION=\"0.0.1\"
-DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1
-DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1
-DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_DLFCN_H=1 -I. -I/home/craig/source_code/c++/src/cyclic_namespaces/src
-DHAVE_CONFIG_H -I/home/craig/source_code/c++/src/cyclic_namespaces/include
-g -O2 -MT main-main.o -MD -MP -MF .deps/main-main.Tpo -c -o main-main.o
`test -f 'main.cc' || echo
'/home/craig/source_code/c++/src/cyclic_namespaces/src/'`main.cc

In file included from
/home/craig/source_code/c++/src/cyclic_namespaces/include/classA.h:4,

from /home/craig/source_code/c++/src/cyclic_namespaces/src/main.cc:1:

/home/craig/source_code/c++/src/cyclic_namespaces/include/classB.h:14:
error: `

ns1' was not declared in this scope

/home/craig/source_code/c++/src/cyclic_namespaces/include/classB.h:14:
error: syntax

error before `::' token

make[1]: *** [main-main.o] Error 1

make[1]: Leaving directory
`/home/craig/source_code/c++/build/cyclic_namespaces/src'

make: *** [all-recursive] Error 1

The 2 offending files seem to be classA.h and classB.h which are implemented
as follows.

File : classA.h

===============

#ifndef CLASSA_H

#define CLASSA_H

#include <classB.h>


The basic problem is here. The other header file includes this
file, but because of the #ifndef guard will have no effect.

Try a forward declaration in one header file so you don't need
to include the other. For example, place the following in classA.h:

namespace ns3 {
namespace ns4 {
class classB;
}
}

You may even put your forward declarations in a separate header
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top