0

I have two classes with their respective namespace.
Class A has an object of class B, and can call methods from class B.


I want class B to call a method from class A as well, for that, I include "classA.h" in classB.h, but when I do that I get an error saying..

error C2653: 'classB_namespace' : is not a class or namespace name

I am using win32 sdk for this project and I have defined WinMain() in classA.cpp, in WinMain() I have an object of classA, which calls wndProct defined in classB.

I want to access this classA object from classB (so as to call that method in classA which is why I am doing all this..).

How, and more importantly, can it be done?? I used extern on the classA object in WinMain() didn't work.

So the bottomline is, I am not able to call that method from classB, which is defined in classA, using the object defined in WinMain().

2am
  • 699
  • 1
  • 7
  • 25
  • 5
    We need the code you are talking about to give you an answer. – Stefano Sanfilippo Oct 13 '13 at 10:45
  • 1
    Class A has an object of B and also B want to include A and i think circular '#include' can not be done. – MRB Oct 13 '13 at 10:51
  • 1
    [Circular dependency](http://en.wikipedia.org/wiki/Circular_dependency#Example_of_circular_dependencies_in_C.2B.2B) and [Forward declaration](http://en.wikipedia.org/wiki/Forward_declaration). – masoud Oct 13 '13 at 11:02

1 Answers1

2

It is likely (but not definitely, without the seeing code) this problem have arised because C++ is compiled in one pass. So, #include is not like usage-directive in other languages (like C# or java) - you must keep track of all definitions to be in the right order - every class must be declared (or even fully defined) before it is used, no matter if code is in single file or multiple incuded headers. And namespaces does not change things much - order of definitions must be kept.

This is wrong code, because class B is mentioned before it is declared:

class A {
  B objB;
};

class B {
  A *parent;
};

The correct declaration order is:

class A;

class B {
  A *parent;
};

class A {
  B objB;
};

Or, with each class in separate namespace, it would be like this:

namespace classA_namespace {
  class A;
}

namespace classB_namespace {
  class B {
    classA_namespace::A *parent;
  };
}

namespace classA_namespace {
  class A {
    classB_namespace::B objB;
  };
}
mas.morozov
  • 2,666
  • 1
  • 22
  • 22
  • Yes.. thats the issue.. as @M M and @Mohammad described above,. its a circular dependency issue. what I need is more practice DESIGNING in c++. I have been coding in C for all my life. it will take some time to get used. Thanks for help. – 2am Oct 13 '13 at 11:20
  • 1
    @2am One little fact about C++ design patterns - having each class in its separate namespace is not common or good practice. Namespaces are used to group related classes (like functions are to group related code or classes are to group related functions) and usually one namespace contains much more than one class (the example is `std` namespace) – mas.morozov Oct 13 '13 at 11:33
  • Yes, thats what I have observed over last couple of weeks :P Anyhow, could you recommend any good C++ design patterns books? I am good with c++ programming but the designing part is where I have to work more. – 2am Oct 14 '13 at 04:23
  • Of course, I can. And not only me - http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – mas.morozov Oct 14 '13 at 09:38