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 definition issue

Class definition issue

Scheduled Pinned Locked Moved C / C++ / MFC
helpcareerworkspace
3 Posts 2 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.
  • P Offline
    P Offline
    Patrick G
    wrote on last edited by
    #1

    Hello, I'm trying to recreate a problem that I'm having in another project and have created a small overly simple project to experiment with. That being said, I'm being blocked by what seems like a really silly problem. So, I have two classes: ClassA and ClassB. ClassB's constructor takes a pointer to ClassA as a parameter for later use. The compiler seems not to mind that, however it does not like when I try to use the pointer that is passed. Here's some code:

    //ClassA.h
    #pragma once;

    #include <iostream>
    using namespace std;

    #include "ClassB.h"
    class ClassB;

    class ClassA {
    public:
    ClassA () {
    this->b = new ClassB(this);
    }

    void printer() {
    	cout << "Hello world from A!\\n";
    }
    

    private:
    ClassB* b;

    };

    And a little more:

    //ClassB.h
    #pragma once;

    #include <iostream>
    using namespace std;

    #include "ClassA.h"
    class ClassA;

    class ClassB {
    public:
    ClassB(ClassA *a) {
    this->ca = a;
    cout << "Constructor B \n\n";
    ca->printer();
    }

    private:
    ClassA *ca;
    };

    and finally a main to polish it off:

    #include <windows.h>

    #include <iostream>
    using namespace std;

    #include "ClassA.h"
    #include "ClassB.h"

    void main() {
    ClassA a;
    Sleep(10000);
    }

    I get two errors with this setup. They both point to ca->printer(); in the ClassB.h file. They are Error 1 error C2027: use of undefined type 'ClassA' classb.h 14 and Error 2 error C2227: left of '->printer' must point to class/struct/union/generic type classb.h 14 . Oddly enough, this isn't even the problem I'm trying to recreate from my larger project (I don't have this problem there). I'm actually trying to recreate an access violation on the call to the constructor of the class that ClassB represents (then the program continues, but it never executes the constructor). Anyway, I'll settle for figuring out what the silly mistake I'm making is that's blocking the compiler from finishing the job. Thanks for your help!

    C 1 Reply Last reply
    0
    • P Patrick G

      Hello, I'm trying to recreate a problem that I'm having in another project and have created a small overly simple project to experiment with. That being said, I'm being blocked by what seems like a really silly problem. So, I have two classes: ClassA and ClassB. ClassB's constructor takes a pointer to ClassA as a parameter for later use. The compiler seems not to mind that, however it does not like when I try to use the pointer that is passed. Here's some code:

      //ClassA.h
      #pragma once;

      #include <iostream>
      using namespace std;

      #include "ClassB.h"
      class ClassB;

      class ClassA {
      public:
      ClassA () {
      this->b = new ClassB(this);
      }

      void printer() {
      	cout << "Hello world from A!\\n";
      }
      

      private:
      ClassB* b;

      };

      And a little more:

      //ClassB.h
      #pragma once;

      #include <iostream>
      using namespace std;

      #include "ClassA.h"
      class ClassA;

      class ClassB {
      public:
      ClassB(ClassA *a) {
      this->ca = a;
      cout << "Constructor B \n\n";
      ca->printer();
      }

      private:
      ClassA *ca;
      };

      and finally a main to polish it off:

      #include <windows.h>

      #include <iostream>
      using namespace std;

      #include "ClassA.h"
      #include "ClassB.h"

      void main() {
      ClassA a;
      Sleep(10000);
      }

      I get two errors with this setup. They both point to ca->printer(); in the ClassB.h file. They are Error 1 error C2027: use of undefined type 'ClassA' classb.h 14 and Error 2 error C2227: left of '->printer' must point to class/struct/union/generic type classb.h 14 . Oddly enough, this isn't even the problem I'm trying to recreate from my larger project (I don't have this problem there). I'm actually trying to recreate an access violation on the call to the constructor of the class that ClassB represents (then the program continues, but it never executes the constructor). Anyway, I'll settle for figuring out what the silly mistake I'm making is that's blocking the compiler from finishing the job. Thanks for your help!

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      You will need to move some code into a cpp file (or two): to be able to use a pointer for a class, the compiler needs to fully know the class. In your case, both classes are referencing each other and using the pointers in the header file. It's a snake that bites its own tail.

      Cédric Moonen Software developer
      Charting control [v1.4] OpenGL game tutorial in C++

      P 1 Reply Last reply
      0
      • C Cedric Moonen

        You will need to move some code into a cpp file (or two): to be able to use a pointer for a class, the compiler needs to fully know the class. In your case, both classes are referencing each other and using the pointers in the header file. It's a snake that bites its own tail.

        Cédric Moonen Software developer
        Charting control [v1.4] OpenGL game tutorial in C++

        P Offline
        P Offline
        Patrick G
        wrote on last edited by
        #3

        Thanks for the tip, you were correct. Now I get to go back to recreating my real problem... the phantom access violation!

        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