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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with multiple classes

Help with multiple classes

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionhelp
5 Posts 4 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.
  • X Offline
    X Offline
    Xds
    wrote on last edited by
    #1

    Hi all, This is my first post; I'm new to C++ so go easy on me. I have two classes; and their functions include code that refer to the other: i.e: Class X has code that uses a variable/function from Class Y and vice-versa. The classes are completely different, so it would not help to derive one off the other. In Header.h:

    ------------------------------------------------------------
    class X {
    public:
    int myint;
    void doit(void);
    };

    void X::doit(void) {
    printf("CY.myint = %i", CY.myint); // Print Class Y's myint
    }

    class Y {
    public:
    int myint;
    void doit(void);
    };

    void Y::doit(void) {
    printf("CX.myint = %i", CX.myint); // Print Class X's myint
    }

    In Main.CPP:

    ------------------------------------------------------------
    #include "stdio.h"
    #include "Header.h"

    int main(void) {

    class X CX;
    class Y CY;
    
    CX.myint = 100;
    CY.myint = 200;
    CX.doit();
    CY.doit();
    
    return 1;
    

    }

    How can I get these classes to work together? I can't put an 'extern class Y' before the 'class X' declaration, so how can I let the compiler know that the other class exists and will be declared? Thanks! Mike

    R R L 3 Replies Last reply
    0
    • X Xds

      Hi all, This is my first post; I'm new to C++ so go easy on me. I have two classes; and their functions include code that refer to the other: i.e: Class X has code that uses a variable/function from Class Y and vice-versa. The classes are completely different, so it would not help to derive one off the other. In Header.h:

      ------------------------------------------------------------
      class X {
      public:
      int myint;
      void doit(void);
      };

      void X::doit(void) {
      printf("CY.myint = %i", CY.myint); // Print Class Y's myint
      }

      class Y {
      public:
      int myint;
      void doit(void);
      };

      void Y::doit(void) {
      printf("CX.myint = %i", CX.myint); // Print Class X's myint
      }

      In Main.CPP:

      ------------------------------------------------------------
      #include "stdio.h"
      #include "Header.h"

      int main(void) {

      class X CX;
      class Y CY;
      
      CX.myint = 100;
      CY.myint = 200;
      CX.doit();
      CY.doit();
      
      return 1;
      

      }

      How can I get these classes to work together? I can't put an 'extern class Y' before the 'class X' declaration, so how can I let the compiler know that the other class exists and will be declared? Thanks! Mike

      R Offline
      R Offline
      Rickard Andersson20
      wrote on last edited by
      #2

      Xds wrote: int main(void) { class X CX; class Y CY; CX.myint = 100; CY.myint = 200; CX.doit(); CY.doit(); return 1; } I'm not sure about this but isn't it "wrong" to write 'class X CX'? Isn't 'X CX' the right way!? I can be wrong!!!!!! ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

      X 1 Reply Last reply
      0
      • R Rickard Andersson20

        Xds wrote: int main(void) { class X CX; class Y CY; CX.myint = 100; CY.myint = 200; CX.doit(); CY.doit(); return 1; } I'm not sure about this but isn't it "wrong" to write 'class X CX'? Isn't 'X CX' the right way!? I can be wrong!!!!!! ------------------------------------ Rickard Andersson, Suza Computing ICQ#: 50302279 I'm from the winter country SWEDEN! ------------------------------------

        X Offline
        X Offline
        Xds
        wrote on last edited by
        #3

        Pretty sure it works either way, but in any case, that's not the problem. Thanks! Mike

        1 Reply Last reply
        0
        • X Xds

          Hi all, This is my first post; I'm new to C++ so go easy on me. I have two classes; and their functions include code that refer to the other: i.e: Class X has code that uses a variable/function from Class Y and vice-versa. The classes are completely different, so it would not help to derive one off the other. In Header.h:

          ------------------------------------------------------------
          class X {
          public:
          int myint;
          void doit(void);
          };

          void X::doit(void) {
          printf("CY.myint = %i", CY.myint); // Print Class Y's myint
          }

          class Y {
          public:
          int myint;
          void doit(void);
          };

          void Y::doit(void) {
          printf("CX.myint = %i", CX.myint); // Print Class X's myint
          }

          In Main.CPP:

          ------------------------------------------------------------
          #include "stdio.h"
          #include "Header.h"

          int main(void) {

          class X CX;
          class Y CY;
          
          CX.myint = 100;
          CY.myint = 200;
          CX.doit();
          CY.doit();
          
          return 1;
          

          }

          How can I get these classes to work together? I can't put an 'extern class Y' before the 'class X' declaration, so how can I let the compiler know that the other class exists and will be declared? Thanks! Mike

          R Offline
          R Offline
          Roger Allen
          wrote on last edited by
          #4

          You need to use a forward declaration of the class.

          class Y ; // forward class declaration

          class X
          {
          void DoIt(Y& y) ; // pass in a reference to a Y object
          } ;

          class Y
          {
          } ;

          You also cannot print out the content of CY in CX unless you pass a reference / pointer / copy of the object you want to print in as a parameter. The CX in main does not know anything about the CY:

          #include "stdio.h"
          #include "Header.h"
          int main(void)
          {
          X CX;
          Y CY;
          CX.myint = 100;
          CY.myint = 200;
          CX.doit(CY); // see class declaration above
          CY.doit();
          return 1;
          }

          Roger Allen Sonork 100.10016 If I'm not breathing, I'm either dead or holding my breath. A fool jabbers, while a wise man listens. But is he so wise to listen to the fool?

          1 Reply Last reply
          0
          • X Xds

            Hi all, This is my first post; I'm new to C++ so go easy on me. I have two classes; and their functions include code that refer to the other: i.e: Class X has code that uses a variable/function from Class Y and vice-versa. The classes are completely different, so it would not help to derive one off the other. In Header.h:

            ------------------------------------------------------------
            class X {
            public:
            int myint;
            void doit(void);
            };

            void X::doit(void) {
            printf("CY.myint = %i", CY.myint); // Print Class Y's myint
            }

            class Y {
            public:
            int myint;
            void doit(void);
            };

            void Y::doit(void) {
            printf("CX.myint = %i", CX.myint); // Print Class X's myint
            }

            In Main.CPP:

            ------------------------------------------------------------
            #include "stdio.h"
            #include "Header.h"

            int main(void) {

            class X CX;
            class Y CY;
            
            CX.myint = 100;
            CY.myint = 200;
            CX.doit();
            CY.doit();
            
            return 1;
            

            }

            How can I get these classes to work together? I can't put an 'extern class Y' before the 'class X' declaration, so how can I let the compiler know that the other class exists and will be declared? Thanks! Mike

            L Offline
            L Offline
            Le centriste
            wrote on last edited by
            #5

            That code wont't compile because in your doit methods you reference hard-coded instances. Michel If I am wrong or said something stupid, I apologize in advance ;)

            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