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. A class within a class

A class within a class

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionlounge
10 Posts 3 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.
  • C Offline
    C Offline
    Chris Copeland
    wrote on last edited by
    #1

    Hey. I'm trying to store a general, overall class that will store other classes within for a much easier accessible object, however, I keep running into the same problem. Here's a small example of the code i'm trying to produce:

    class A {
    public:
    A();
    ~A();
    };

    class B {
    public:
    B();
    ~B();
    A Foo;
    };

    And it keeps throwing the same error at me, that there's an unrecognised identifier before the variable 'Foo', and that default-int isn't supported (obviously). Class A is in a seperate header file, but i'm including it at the top of the header file for Class B. However, no matter what I try, it refuses to believe that 'class A' exists, and can't reference to it. I've even tried using the typedef keyword to declare 'class A' as an identifier, but even that won't work. I can't find any results over Google, nor here, about this problem. Can anyone assist? Thanks in advance.

    C S 2 Replies Last reply
    0
    • C Chris Copeland

      Hey. I'm trying to store a general, overall class that will store other classes within for a much easier accessible object, however, I keep running into the same problem. Here's a small example of the code i'm trying to produce:

      class A {
      public:
      A();
      ~A();
      };

      class B {
      public:
      B();
      ~B();
      A Foo;
      };

      And it keeps throwing the same error at me, that there's an unrecognised identifier before the variable 'Foo', and that default-int isn't supported (obviously). Class A is in a seperate header file, but i'm including it at the top of the header file for Class B. However, no matter what I try, it refuses to believe that 'class A' exists, and can't reference to it. I've even tried using the typedef keyword to declare 'class A' as an identifier, but even that won't work. I can't find any results over Google, nor here, about this problem. Can anyone assist? Thanks in advance.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      the following project

      // A.h
      #pragma once

      class A {
      public:
      A();
      ~A();
      };

      // B.h
      #pragma once

      class B {
      public:
      B();
      ~B();
      A Foo;
      };

      // A.cpp
      #include "A.h"

      A::A(){}
      A::~A(){}

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

      B::~B(){}
      B::B(){}

      // APP.cpp
      #include "A.h"
      #include "B.h"

      void main()
      {
      A a;
      B b;
      }

      compiles fine even with VC++ 6.0. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • C Chris Copeland

        Hey. I'm trying to store a general, overall class that will store other classes within for a much easier accessible object, however, I keep running into the same problem. Here's a small example of the code i'm trying to produce:

        class A {
        public:
        A();
        ~A();
        };

        class B {
        public:
        B();
        ~B();
        A Foo;
        };

        And it keeps throwing the same error at me, that there's an unrecognised identifier before the variable 'Foo', and that default-int isn't supported (obviously). Class A is in a seperate header file, but i'm including it at the top of the header file for Class B. However, no matter what I try, it refuses to believe that 'class A' exists, and can't reference to it. I've even tried using the typedef keyword to declare 'class A' as an identifier, but even that won't work. I can't find any results over Google, nor here, about this problem. Can anyone assist? Thanks in advance.

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

        This code (all in one file) compiles fine (with g++, but msvc will compile it as well) for me. There must be something else stopping B seeing A.

        class A
        {
        public:
        A() {}
        ~A() {}
        };

        class B
        {
        public:
        B() {}
        ~B() {}
        A foo;
        };

        One thing you could try is just running the code through the pre-processor (cl -E, if I remember rightly) to see what the compiler really sees.

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

        C 1 Reply Last reply
        0
        • S Stuart Dootson

          This code (all in one file) compiles fine (with g++, but msvc will compile it as well) for me. There must be something else stopping B seeing A.

          class A
          {
          public:
          A() {}
          ~A() {}
          };

          class B
          {
          public:
          B() {}
          ~B() {}
          A foo;
          };

          One thing you could try is just running the code through the pre-processor (cl -E, if I remember rightly) to see what the compiler really sees.

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

          C Offline
          C Offline
          Chris Copeland
          wrote on last edited by
          #4

          Hm, running it through the pre-processor didn't really do much, since i'm pretty new to C++. All the headers are linked though. The header file which requires class 'A' has the inclusion for class A in the header. To be honest, I don't understand how it wouldn't be linking them together, they're all connected and 'class A' is definately being declared in (eg) 'A.h'. 'B.h' then includes 'A.h', and then requires 'A Foo' further down in the code. There are appropriate .cpp files that are also used with the initializers etc. and i've included both 'A.h' *and* 'B.h' in both of those files. It's just refusing to recognise 'class A' as any type of identifier, yet if I hover over the keyword 'A', it tells me that it's a class etc.

          S 1 Reply Last reply
          0
          • C Chris Copeland

            Hm, running it through the pre-processor didn't really do much, since i'm pretty new to C++. All the headers are linked though. The header file which requires class 'A' has the inclusion for class A in the header. To be honest, I don't understand how it wouldn't be linking them together, they're all connected and 'class A' is definately being declared in (eg) 'A.h'. 'B.h' then includes 'A.h', and then requires 'A Foo' further down in the code. There are appropriate .cpp files that are also used with the initializers etc. and i've included both 'A.h' *and* 'B.h' in both of those files. It's just refusing to recognise 'class A' as any type of identifier, yet if I hover over the keyword 'A', it tells me that it's a class etc.

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

            Can you post the content of a.h, b.h?

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

            C 1 Reply Last reply
            0
            • S Stuart Dootson

              Can you post the content of a.h, b.h?

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

              C Offline
              C Offline
              Chris Copeland
              wrote on last edited by
              #6

              Well, i've found a sort-of solution now. I'm currently declaring "class A;" after the "B.h" inclusion, but i'm going to run through my headers and make sure they're not getting mixed up. It might be that they're linked incorrectly (even if they're linked in the files that need them).

              S 1 Reply Last reply
              0
              • C Chris Copeland

                Well, i've found a sort-of solution now. I'm currently declaring "class A;" after the "B.h" inclusion, but i'm going to run through my headers and make sure they're not getting mixed up. It might be that they're linked incorrectly (even if they're linked in the files that need them).

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

                Having a forward declaration for A (that's the 'class A;') shouldn't help in the definition of B - B would need to see the full definition of A to have a member of type A. Oh well. If you've got it to work...

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

                C 1 Reply Last reply
                0
                • S Stuart Dootson

                  Having a forward declaration for A (that's the 'class A;') shouldn't help in the definition of B - B would need to see the full definition of A to have a member of type A. Oh well. If you've got it to work...

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

                  C Offline
                  C Offline
                  Chris Copeland
                  wrote on last edited by
                  #8

                  My thoughts exactly. However, I placed #ifndef _SOCKETS_OBJ_DECLARED_ around the 'class A;', and in the 'B.h' at the end, I added #define _SOCKETS_OBJ_DECLARED_ Then, when I placed an #error "Undeclared" in the #ifndef, it threw the error.. so for some reason, the header is being included but it's not globally declaring the 'class B'. It's really strange, and another programmer said he couldn't explain it. The best I can do is go through it with a fine toothcomb and hopefully find where it's not declaring properly.

                  S 1 Reply Last reply
                  0
                  • C Chris Copeland

                    My thoughts exactly. However, I placed #ifndef _SOCKETS_OBJ_DECLARED_ around the 'class A;', and in the 'B.h' at the end, I added #define _SOCKETS_OBJ_DECLARED_ Then, when I placed an #error "Undeclared" in the #ifndef, it threw the error.. so for some reason, the header is being included but it's not globally declaring the 'class B'. It's really strange, and another programmer said he couldn't explain it. The best I can do is go through it with a fine toothcomb and hopefully find where it's not declaring properly.

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

                    I wonder if you've just got a missing semi-colon or something somewhere - that can cause really bizarre error messages and can be really hard to spot.

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

                    C 1 Reply Last reply
                    0
                    • S Stuart Dootson

                      I wonder if you've just got a missing semi-colon or something somewhere - that can cause really bizarre error messages and can be really hard to spot.

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

                      C Offline
                      C Offline
                      Chris Copeland
                      wrote on last edited by
                      #10

                      I have no idea. I've been rooting through these header files, and as far as I can tell there are no missing semi-colons. In Visual C++, the actual region inside #ifndef _PACKETS_OBJ_DECLARED_ is completely grayed out, suggesting it should never be called, however it *is* being called, which might be something to do at compilation time. This has absolutely lost me, lol.

                      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