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. class referencing problem

class referencing problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++tutorial
7 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.
  • Z Offline
    Z Offline
    zildjohn01
    wrote on last edited by
    #1

    hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code:: main.cpp #include "class1.h" void main() { class1 test; } class1.h #pragma once #include "class2.h" class class1 { private: class2 *child1; class2 *child2; public: class1() { child1 = new class2(); child1->Init(); child2 = new class2(); child2->Init(); } }; class1.cpp #include "class1.h" class2.h #pragma once #include "class1.h" class class2 { private: class1 *parent; public: void Init(class1 *the_parent) { parent = the_parent; } }; class2.cpp #include "class2.h" Here is a snapshot of compiler output: Compiling... class2.cpp class1.h(5) : error C2143: syntax error : missing ';' before '*' class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers class1.h(6) : error C2143: syntax error : missing ';' before '*' class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers main.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' class1_1.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' one class's signature must be declared before the other, but they both reference each other.. how can this be done?

    C J J 3 Replies Last reply
    0
    • Z zildjohn01

      hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code:: main.cpp #include "class1.h" void main() { class1 test; } class1.h #pragma once #include "class2.h" class class1 { private: class2 *child1; class2 *child2; public: class1() { child1 = new class2(); child1->Init(); child2 = new class2(); child2->Init(); } }; class1.cpp #include "class1.h" class2.h #pragma once #include "class1.h" class class2 { private: class1 *parent; public: void Init(class1 *the_parent) { parent = the_parent; } }; class2.cpp #include "class2.h" Here is a snapshot of compiler output: Compiling... class2.cpp class1.h(5) : error C2143: syntax error : missing ';' before '*' class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers class1.h(6) : error C2143: syntax error : missing ';' before '*' class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers main.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' class1_1.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' one class's signature must be declared before the other, but they both reference each other.. how can this be done?

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      class1.h
      #pragma once
      #include "class2.h"

      class class2; // <-- forward declaration. do the same thing in class2.h

      class class1 {
      private:
      class2 *child1;
      class2 *child2;
      public:
      class1()
      {
      child1 = new class2();
      child1->Init();
      child2 = new class2();
      child2->Init();
      }
      };

      Cleek | Image Toolkits | Thumbnail maker

      J 1 Reply Last reply
      0
      • Z zildjohn01

        hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code:: main.cpp #include "class1.h" void main() { class1 test; } class1.h #pragma once #include "class2.h" class class1 { private: class2 *child1; class2 *child2; public: class1() { child1 = new class2(); child1->Init(); child2 = new class2(); child2->Init(); } }; class1.cpp #include "class1.h" class2.h #pragma once #include "class1.h" class class2 { private: class1 *parent; public: void Init(class1 *the_parent) { parent = the_parent; } }; class2.cpp #include "class2.h" Here is a snapshot of compiler output: Compiling... class2.cpp class1.h(5) : error C2143: syntax error : missing ';' before '*' class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers class1.h(6) : error C2143: syntax error : missing ';' before '*' class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers main.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' class1_1.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' one class's signature must be declared before the other, but they both reference each other.. how can this be done?

        J Offline
        J Offline
        Jose Lamas Rios
        wrote on last edited by
        #3

        You need to use forward declarations. Try this: // class1.h #pragma once class class2; class class1 { private: class2* child1; class2* child2; public: class1() { child1 = new class2(this); child2 = new class2(this); } }; // class2.h #pragma once class class1; class class2 { private: class1* parent; public: class2(class1* the_parent) : parent(the_parent) { } }; -- jlr http://jlamas.blogspot.com/[^]

        1 Reply Last reply
        0
        • C Chris Losinger

          class1.h
          #pragma once
          #include "class2.h"

          class class2; // <-- forward declaration. do the same thing in class2.h

          class class1 {
          private:
          class2 *child1;
          class2 *child2;
          public:
          class1()
          {
          child1 = new class2();
          child1->Init();
          child2 = new class2();
          child2->Init();
          }
          };

          Cleek | Image Toolkits | Thumbnail maker

          J Offline
          J Offline
          John Simon
          wrote on last edited by
          #4

          thanks so much! first time i got an answer in a forum in only 2 minutes :-D

          1 Reply Last reply
          0
          • Z zildjohn01

            hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code:: main.cpp #include "class1.h" void main() { class1 test; } class1.h #pragma once #include "class2.h" class class1 { private: class2 *child1; class2 *child2; public: class1() { child1 = new class2(); child1->Init(); child2 = new class2(); child2->Init(); } }; class1.cpp #include "class1.h" class2.h #pragma once #include "class1.h" class class2 { private: class1 *parent; public: void Init(class1 *the_parent) { parent = the_parent; } }; class2.cpp #include "class2.h" Here is a snapshot of compiler output: Compiling... class2.cpp class1.h(5) : error C2143: syntax error : missing ';' before '*' class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers class1.h(6) : error C2143: syntax error : missing ';' before '*' class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers main.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' class1_1.cpp class2.h(5) : error C2143: syntax error : missing ';' before '*' class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers class2.h(7) : error C2061: syntax error : identifier 'class1' one class's signature must be declared before the other, but they both reference each other.. how can this be done?

            J Offline
            J Offline
            John Simon
            wrote on last edited by
            #5

            that appeared to solve my problem, but now i have a new one. i get error C2512, no default constructor available, but there is a public constructor clearly defined with no parameters for both classes. this error occurs in the constructor for struct1. here is the code: main.cpp #include "class2.h" void main() { struct1 *config; config->item = new class2(); config->item->Init(config); } struct1.h #pragma once #include "class2.h" class class2; struct struct1 { private: class2 *item; public: struct1() { item = new class2(); /* ERROR OCCURS HERE while compiling main.cpp and class2.cpp */ } }; class1.h #pragma once class class1 { public: class1(){} }; class2.h #pragma once #include "class1.h" #include "struct1.h" struct struct1; class class2 : public class1 { private: struct1 *cfg; public: class2() {} void Init(struct1 *data) { cfg = data; } }; struct1.cpp #include "struct1.h" class1.cpp #include "class1.h" class2.cpp #include "class2.h"

            J 1 Reply Last reply
            0
            • J John Simon

              that appeared to solve my problem, but now i have a new one. i get error C2512, no default constructor available, but there is a public constructor clearly defined with no parameters for both classes. this error occurs in the constructor for struct1. here is the code: main.cpp #include "class2.h" void main() { struct1 *config; config->item = new class2(); config->item->Init(config); } struct1.h #pragma once #include "class2.h" class class2; struct struct1 { private: class2 *item; public: struct1() { item = new class2(); /* ERROR OCCURS HERE while compiling main.cpp and class2.cpp */ } }; class1.h #pragma once class class1 { public: class1(){} }; class2.h #pragma once #include "class1.h" #include "struct1.h" struct struct1; class class2 : public class1 { private: struct1 *cfg; public: class2() {} void Init(struct1 *data) { cfg = data; } }; struct1.cpp #include "struct1.h" class1.cpp #include "class1.h" class2.cpp #include "class2.h"

              J Offline
              J Offline
              Jose Lamas Rios
              wrote on last edited by
              #6

              The way you are mixing #includes and forward declarations is wrong. For example, take a look at what the compiler is seeing when you ask it to compile class2.cpp

              • finds an include for "class2.h", so it goes for it.
              • finds an include for "class1.h", so it goes for it.
              • compiles the declaration for class1. Nothing else in this file, so it returns to class2.h
              • finds an include for "struct1.h", so it goes for it.
              • finds an include for "class2.h", but it's already started processing that file, and noted a "pragma once" directive, so it ignores this include.
              • finds a forward declaration for class2, so it says to itself "I now know class2 is a class, even if I still don't know all the details"
              • starts compiling the declaration for struct1. Finds the declaration of "item" as a pointer to a "class2". At this point, the compiler only needs to know wether the type of "item" is valid, and how much space it takes to store it. It does know the type is valid because of the forward declaration. And it does know how much space is needed for "item", because all pointers are the same size. Compilation goes on.
              • It now reaches the line with "new class2()" in it, so it needs to create a call to some constructor of class2 with no parameters. The problem is it hasn't actually seen the complete declaration of class2 yet, so it doesn't know if such constructor exists

              Reorganize your code as follows:

              ////////////////////////////////////////////////////////
              // class1.h - Put class declaration here

              #pragma once

              class class1
              {
              public:
              class1();

              // Other members' declarations
              };

              ////////////////////////////////////////////////////////
              // class1.cpp - Put class implementation here

              #include "class1.h"

              class1::class1()
              {
              }

              // Other members' implementations

              ////////////////////////////////////////////////////////
              // class2.h - Put class declaration here

              #pragma once

              #include "class1.h"

              struct struct1;

              class class2 : public class1
              {
              private:
              struct1* cfg;

              public:
              class2();

              void Init(struct1* data);
              };

              ////////////////////////////////////////////////////////
              // class2.cpp - Put class implementation here

              #include "class2.h"

              class2::class2()
              {
              }

              void class2::Init(struct1* data)
              {
              // Consider moving this to the constructor and
              // removing this function from the class,
              // so that you don't need to always call Init()
              // after the creation of each and ev

              J 1 Reply Last reply
              0
              • J Jose Lamas Rios

                The way you are mixing #includes and forward declarations is wrong. For example, take a look at what the compiler is seeing when you ask it to compile class2.cpp

                • finds an include for "class2.h", so it goes for it.
                • finds an include for "class1.h", so it goes for it.
                • compiles the declaration for class1. Nothing else in this file, so it returns to class2.h
                • finds an include for "struct1.h", so it goes for it.
                • finds an include for "class2.h", but it's already started processing that file, and noted a "pragma once" directive, so it ignores this include.
                • finds a forward declaration for class2, so it says to itself "I now know class2 is a class, even if I still don't know all the details"
                • starts compiling the declaration for struct1. Finds the declaration of "item" as a pointer to a "class2". At this point, the compiler only needs to know wether the type of "item" is valid, and how much space it takes to store it. It does know the type is valid because of the forward declaration. And it does know how much space is needed for "item", because all pointers are the same size. Compilation goes on.
                • It now reaches the line with "new class2()" in it, so it needs to create a call to some constructor of class2 with no parameters. The problem is it hasn't actually seen the complete declaration of class2 yet, so it doesn't know if such constructor exists

                Reorganize your code as follows:

                ////////////////////////////////////////////////////////
                // class1.h - Put class declaration here

                #pragma once

                class class1
                {
                public:
                class1();

                // Other members' declarations
                };

                ////////////////////////////////////////////////////////
                // class1.cpp - Put class implementation here

                #include "class1.h"

                class1::class1()
                {
                }

                // Other members' implementations

                ////////////////////////////////////////////////////////
                // class2.h - Put class declaration here

                #pragma once

                #include "class1.h"

                struct struct1;

                class class2 : public class1
                {
                private:
                struct1* cfg;

                public:
                class2();

                void Init(struct1* data);
                };

                ////////////////////////////////////////////////////////
                // class2.cpp - Put class implementation here

                #include "class2.h"

                class2::class2()
                {
                }

                void class2::Init(struct1* data)
                {
                // Consider moving this to the constructor and
                // removing this function from the class,
                // so that you don't need to always call Init()
                // after the creation of each and ev

                J Offline
                J Offline
                John Simon
                wrote on last edited by
                #7

                ok thank you so much, my code compiles and runs now ;)

                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