stefv
stefv

Reputation: 439

Namespaces and forward classes in C++

I want to use namespaces with my C++ classes but I have multiple problems during the compile step.

Here are my very simple classes with their header file.

Class1.h

#if !defined(CLASS1)
#define CLASS1

namespace Test1
{
   class Test2::Class2;

   class Class1
   {
   public:
      Class1();
        virtual ~Class1();
        Test2::Class2 *getClass2();
   };
}
#endif // !defined(CLASS1)

Class1.cpp

#include "Class1.h"
#include "Class2.h"

using namespace Test1;
using namespace Test2;

Class1::Class1(){
}

Class1::~Class1(){
}

Class2 *Class1::getClass2(){
  return new Class2();
}

Class2.h

#if !defined(CLASS2)
#define CLASS2

namespace Test2
{
  class Test1::Class1;

  class Class2
  {
  public:
    Class2();
    virtual ~Class2();
    Test1::Class1 *getClass1();
  };
}
#endif // !defined(CLASS2)

Class2.cpp

#include "Class2.h"
#include "Class1.h"

using namespace Test1;
using namespace Test2;

Class2::Class2(){
}

Class2::~Class2(){
}

Class1 *Class2::getClass1(){
   return new Class1();
}

If I'm not using the namespaces, I can compile the classes without any error. But If I'm trying to use the namespace, I have these errors:

1>------ Rebuild All started: Project: TestsIncludes, Configuration: Debug Win32 ------
1>TestsIncludes.cpp
1>stdafx.cpp
1>Class2.cpp
1>c:\users\stefv\test\class2.h(6): error C2653: 'Test1': is not a class or namespace name
1>c:\users\stefv\test\class2.h(6): error C2079: 'Class1' uses undefined class 'Test2::Test1'
1>c:\users\stefv\test\class2.h(13): error C2027: use of undefined type 'Test2::Test1'
1>c:\users\stefv\test\class2.h(6): note: see declaration of 'Test2::Test1'
1>c:\users\stefv\test\class2.h(13): error C2143: syntax error: missing ';' before '*'
1>c:\users\stefv\test\class2.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\stefv\test\class2.h(13): error C2238: unexpected token(s) preceding ';'
1>c:\users\stefv\test\class2.cpp(13): error C2872: 'Class1': ambiguous symbol
1>c:\users\stefv\test\class2.h(6): note: could be 'int Test2::Class1'
1>c:\users\stefv\test\class1.h(9): note: or       'Test1::Class1'
1>c:\users\stefv\test\class2.cpp(13): error C2143: syntax error: missing ';' before '*'
1>c:\users\stefv\test\class2.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\stefv\test\class2.cpp(13): error C2039: 'getClass1': is not a member of 'Test2::Class2'
1>c:\users\stefv\test\class2.h(9): note: see declaration of 'Test2::Class2'
1>c:\users\stefv\test\class2.cpp(13): error C2059: syntax error: '{'
1>c:\users\stefv\test\class2.cpp(13): error C2143: syntax error: missing ';' before '{'
1>c:\users\stefv\test\class2.cpp(13): error C2447: '{': missing function header (old-style formal list?)
1>Class1.cpp
1>c:\users\stefv\test\class1.h(6): error C2653: 'Test2': is not a class or namespace name
1>c:\users\stefv\test\class1.h(6): error C2079: 'Class2' uses undefined class 'Test1::Test2'
1>c:\users\stefv\test\class1.h(13): error C2027: use of undefined type 'Test1::Test2'
1>c:\users\stefv\test\class1.h(6): note: see declaration of 'Test1::Test2'
1>c:\users\stefv\test\class1.h(13): error C2143: syntax error: missing ';' before '*'
1>c:\users\stefv\test\class1.h(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\stefv\test\class1.h(13): error C2238: unexpected token(s) preceding ';'
1>c:\users\stefv\test\class1.cpp(13): error C2872: 'Class2': ambiguous symbol
1>c:\users\stefv\test\class1.h(6): note: could be 'int Test1::Class2'
1>c:\users\stefv\test\class2.h(9): note: or       'Test2::Class2'
1>c:\users\stefv\test\class1.cpp(13): error C2143: syntax error: missing ';' before '*'
1>c:\users\stefv\test\class1.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\stefv\test\class1.cpp(13): error C2039: 'getClass2': is not a member of 'Test1::Class1'
1>c:\users\stefv\test\class1.h(9): note: see declaration of 'Test1::Class1'
1>c:\users\stefv\test\class1.cpp(13): error C2059: syntax error: '{'
1>c:\users\stefv\test\class1.cpp(13): error C2143: syntax error: missing ';' before '{'
1>c:\users\stefv\test\class1.cpp(13): error C2447: '{': missing function header (old-style formal list?)
1>Generating Code...
1>Done building project "TestsIncludes.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Where are my mistakes with the namespaces or forward classes ?

Thank you for your help !

Upvotes: 0

Views: 238

Answers (1)

piwi
piwi

Reputation: 5336

Your problem is with the way you forward-declare your classes. Instead of:

namespace Test1
{
   class Test2::Class2;
}

You should declare it like this:

namespace Test2
{
  class Class2;
}

Outside of namespace Test1. When you declare class Test2::Class2 you are telling the compiler that you have a class named Class2 inside class Test2, which is not the same, obviously.

Upvotes: 3

Related Questions