C++ How to have a class relying on a namespace and that namespace relying on the class? -


so have class member variables instances of structure defined within namespace, , function within same namespace has parameter pointer instance of above mentioned class.

this looks like:

someclass.h

#ifndef some_class_h #define some_class_h  include "somenamespace.h"  class someclass { private:     somenamespace::somestructure instance1, instance2;      ... 

somenamespace.h

#ifndef some_namespace_h #define some_namespace_h  #include "someclass.h"  namespace somenamespace {     namespace anothernamespace     {         void somefunction( someclass *psomeclass );     }      struct somestructure     {         ...     }     ... 

the errors receiving:

error c2065 'someclass': undeclared identifier   error c2653 'somenamespace' : not class or namespace name 

the first error related to:

void somefunction( someclass *psomeclass ); 

the second error relates to:

somenamespace::somestructure instance1, instance2; 

i fixed first error adding forward declaration 'class someclass;' top of file:

somenamespace.h

#ifndef some_namespace_h #define some_namespace_h  #include "someclass.h"  class someclass;  namespace somenamespace {     namespace anothernamespace     {         void somefunction( someclass *psomeclass );     }      struct somestructure     {         ...     }     ... 

i attempted fix error 2 doing same thing namespace , structure:

someclass.h

#ifndef some_class_h #define some_class_h  include "somenamespace.h"  namespace somenamespace {     struct somestructure; }  class someclass { private:     somenamespace::somestructure instance1, instance2;      ... 

doing forward declaration on namespace , structure within gives me these errors:

'someclass::instance1' uses undefined struct 'somenamespace::somestructure' 'someclass::instance2' uses undefined struct 'somenamespace::somestructure' 

i have searched issue posted user, have been unsuccessful locating post.

if has issue question , feel need rate badly, please add comment why bad question me avoid same mistake next time.

thank in advance help.

based on you've shown us, need forward declaration of someclass in somenamespace.h, not full include:

#ifndef some_namespace_h #define some_namespace_h  // #include "someclass.h"  // << don't this.  class someclass;  namespace somenamespace {     namespace anothernamespace     {         void somefunction( someclass *psomeclass );     } 

the above valid because pointer-to someclass doesn't need know someclass other it's class.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -