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. Include order issues

Include order issues

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 2 Posters 1 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.
  • _ Offline
    _ Offline
    __DanC__
    wrote on last edited by
    #1

    Hi, I'm working with a large project with lots of classes and I have a problem with including the classes. I use include guards at the top of my .h files (I've tried using #pragma once as well) to prevent including more than once. When I include the header of a class I need to use I get an error saying "undefined class", if I was using a pointer I could use a forward declaration but I'm not using a pointer, how do I get around this? Thanks.

    S 1 Reply Last reply
    0
    • _ __DanC__

      Hi, I'm working with a large project with lots of classes and I have a problem with including the classes. I use include guards at the top of my .h files (I've tried using #pragma once as well) to prevent including more than once. When I include the header of a class I need to use I get an error saying "undefined class", if I was using a pointer I could use a forward declaration but I'm not using a pointer, how do I get around this? Thanks.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      It'd help if you posted some code... But if you have something like this:

      // a.h

      #include "b.h"

      class A
      {
      // Stuff
      B b;
      };

      // b.h

      #include "a.h"

      class B
      {
      // Stuff
      A a;
      };

      then you're stuffed - that can't be fixed, because A needs to see a full definition of B before it's defined, and B needs to see a full definition of A before it's defined.

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      _ 1 Reply Last reply
      0
      • S Stuart Dootson

        It'd help if you posted some code... But if you have something like this:

        // a.h

        #include "b.h"

        class A
        {
        // Stuff
        B b;
        };

        // b.h

        #include "a.h"

        class B
        {
        // Stuff
        A a;
        };

        then you're stuffed - that can't be fixed, because A needs to see a full definition of B before it's defined, and B needs to see a full definition of A before it's defined.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        _ Offline
        _ Offline
        __DanC__
        wrote on last edited by
        #3

        Thanks, that makes sense. I hadn't looked at it that simply until I saw that. What would be the "standard" way of creating member variables that reference each other, using your example:

        // a.h

        #include "b.h"

        class B;

        class A
        {
        // Stuff
        B *b;

        CString s1;
        };

        // b.h

        #include "a.h"

        class A;

        class B
        {
        // Stuff
        A *a;

        CString s2;
        };

        That would work now I've used a forward declaration of the class and used a pointer but it seems "messy" to me to have one class as a pointer and the other not. It also seems a waste to have to implement a destructor to clean up the pointers.

        S 1 Reply Last reply
        0
        • _ __DanC__

          Thanks, that makes sense. I hadn't looked at it that simply until I saw that. What would be the "standard" way of creating member variables that reference each other, using your example:

          // a.h

          #include "b.h"

          class B;

          class A
          {
          // Stuff
          B *b;

          CString s1;
          };

          // b.h

          #include "a.h"

          class A;

          class B
          {
          // Stuff
          A *a;

          CString s2;
          };

          That would work now I've used a forward declaration of the class and used a pointer but it seems "messy" to me to have one class as a pointer and the other not. It also seems a waste to have to implement a destructor to clean up the pointers.

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          Pointers are good. An alternative is to use the pimpl idiom[^]:

          // a.h

          class A
          {
          A();
          // Stuff
          private:
          struct A_impl;
          A_impl* impl_;
          };

          // b.h

          class B
          {
          B();
          // Stuff
          private:
          struct B_impl;
          B_impl* impl_;
          };

          // a.cpp
          #include "a.h"
          #include "b.h"
          struct A::A_impl
          {
          B b;
          };

          A::A() : impl_(new A_impl) {}

          // b.cpp
          #include "b.h"
          #include "a.h"
          struct B::B_impl
          {
          A a;
          };

          B::B() : impl_(new B_impl) {}

          In either case, you need to take care with copy constructors and assignment operators.

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

          _ 1 Reply Last reply
          0
          • S Stuart Dootson

            Pointers are good. An alternative is to use the pimpl idiom[^]:

            // a.h

            class A
            {
            A();
            // Stuff
            private:
            struct A_impl;
            A_impl* impl_;
            };

            // b.h

            class B
            {
            B();
            // Stuff
            private:
            struct B_impl;
            B_impl* impl_;
            };

            // a.cpp
            #include "a.h"
            #include "b.h"
            struct A::A_impl
            {
            B b;
            };

            A::A() : impl_(new A_impl) {}

            // b.cpp
            #include "b.h"
            #include "a.h"
            struct B::B_impl
            {
            A a;
            };

            B::B() : impl_(new B_impl) {}

            In either case, you need to take care with copy constructors and assignment operators.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

            _ Offline
            _ Offline
            __DanC__
            wrote on last edited by
            #5

            Thanks for the link, very interesting but I think I'll stick with pointers for 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