Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem with classes interaction

problem with classes interaction

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    singersinger
    wrote on last edited by
    #1

    hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B class A { B* b;}; and class B have a member variable for type class A class B {A a;}; this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A #include "A.h" class B {A a;}; anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learn

    N W S D 4 Replies Last reply
    0
    • S singersinger

      hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B class A { B* b;}; and class B have a member variable for type class A class B {A a;}; this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A #include "A.h" class B {A a;}; anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learn

      N Offline
      N Offline
      Nibu babu thomas
      wrote on last edited by
      #2

      singersinger wrote:

      this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A

      Mr Compiler is right. You need to forward declare class B, which tells the compiler that there is a class called B, and he needn't worry about it.

      class B;

      class A
      { B* b;};

      class B
      {A a;};


      Nibu thomas A Developer Programming tips[^]  My site[^]

      1 Reply Last reply
      0
      • S singersinger

        hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B class A { B* b;}; and class B have a member variable for type class A class B {A a;}; this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A #include "A.h" class B {A a;}; anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learn

        W Offline
        W Offline
        wheelerbarry
        wrote on last edited by
        #3

        Your problem is that a is not defined yet, as the compiler correctly says. The first thing it does when compiling A is go to B and compile that, and thus it does not know that class A exists yet. THe solution to this is to add the prototype to A before including B. e.g. class A; #inlcude "B.h" class A { B* b;};

        M 1 Reply Last reply
        0
        • S singersinger

          hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B class A { B* b;}; and class B have a member variable for type class A class B {A a;}; this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A #include "A.h" class B {A a;}; anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learn

          S Offline
          S Offline
          singersinger
          wrote on last edited by
          #4

          guys, thnx alot 4 ur fast reply but still this doesn't solve the problem, i don't know why :( --------------------- learn, learn, and learn

          1 Reply Last reply
          0
          • W wheelerbarry

            Your problem is that a is not defined yet, as the compiler correctly says. The first thing it does when compiling A is go to B and compile that, and thus it does not know that class A exists yet. THe solution to this is to add the prototype to A before including B. e.g. class A; #inlcude "B.h" class A { B* b;};

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            wheelerbarry wrote:

            class A;

            #inlcude "B.h"

            class A
            { B* b;};

            with this, you will have include problems. in B.h you will need to have include guards

            // B.h
            #ifndef _B_H_
            #define _B_H_
            class B
            {
            // ...
            };
            #endif

            and in A.h it is good practice ( IMO, YMMV ) to also add include guards :

            // A.h
            #ifndef _A_H_
            #define _A_H_

            #ifndef _B_H_
            #include "b.h"
            #endif

            class A
            {
            B* b;
            };
            #endif

            IMO, using forward declaration is the best approach to that problem unless B is not a pointer.


            Maximilien Lincourt Your Head A Splode - Strong Bad

            1 Reply Last reply
            0
            • S singersinger

              hey you guys, the problem is that i have 2 classes (say A,B) , class A have a pointer on class B class A { B* b;}; and class B have a member variable for type class A class B {A a;}; this is seems to be a problem and the compiler retrun an error say 'a' uses undefined class 'A' although i am declaring the header of A #include "A.h" class B {A a;}; anybody have any idea abt wat might be the problem here thnx 4 ur time and concern learn, learn, and learn

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              // A.h
              #pragma once

              class B;

              #include "B.h"

              class A
              {
              B* b;
              };

              // B.h
              #pragma once

              #include "A.h"

              class B
              {
              A a;
              };

              // main.cpp
              #include "B.h"

              void main( void )
              {
              B bb;
              }


              "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

              "Judge not by the eye but by the heart." - Native American Proverb

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups