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. Problem accessing one class from another and vice versa

Problem accessing one class from another and vice versa

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpcsharpquestionannouncement
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.
  • I Offline
    I Offline
    Iceberg76
    wrote on last edited by
    #1

    Hello, I am writing a application in Visual C++ .NET 7.0. I'd like to keep my classes in seperate files to keep things organized, but I am having difficulty accessing classes from within two files. Can someone help me find a solution accessing between files in this manner? I need to access methods and data back and forth in this manner, but I don't want the two classes to be in the same file. What am I doing wrong? Is there a different way to do this? Here is a simplified version: Class1.h #ifndef __Class1H__ #define __Class1H__ #include "Class2.h" class Class1 { public: Class1(); ~Class1(); //This doesn't work because it is in a different file! Class1(Class2* object); //This doesn't work because it is in a different file! Class2 obj; }; #endif // __Class1H__ Class2.h #ifndef __Class2H__ #define __Class2H__ //this is causing problems... #include "Class1.h" class Class2 { public: Class2(); ~Class2(); }; #endif // __Class2H__ Class1.cpp #include "Class1.h" Class1::Class1() { } Class1::~Class1() { } Class2.cpp #include "Class2.h" Class2::Class2() { } Class2::~Class2() { }

    T 2 Replies Last reply
    0
    • I Iceberg76

      Hello, I am writing a application in Visual C++ .NET 7.0. I'd like to keep my classes in seperate files to keep things organized, but I am having difficulty accessing classes from within two files. Can someone help me find a solution accessing between files in this manner? I need to access methods and data back and forth in this manner, but I don't want the two classes to be in the same file. What am I doing wrong? Is there a different way to do this? Here is a simplified version: Class1.h #ifndef __Class1H__ #define __Class1H__ #include "Class2.h" class Class1 { public: Class1(); ~Class1(); //This doesn't work because it is in a different file! Class1(Class2* object); //This doesn't work because it is in a different file! Class2 obj; }; #endif // __Class1H__ Class2.h #ifndef __Class2H__ #define __Class2H__ //this is causing problems... #include "Class1.h" class Class2 { public: Class2(); ~Class2(); }; #endif // __Class2H__ Class1.cpp #include "Class1.h" Class1::Class1() { } Class1::~Class1() { } Class2.cpp #include "Class2.h" Class2::Class2() { } Class2::~Class2() { }

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      first, why do you call #include "Class1.h" into Class2.h when Class2 dont use any object from Classe 1 ?? So, do this :

      Class1.h
      #include "Class2.h";

      class Class1;
      class Class1 {
      Class2 MyClass2Object;
      //...
      }

      Class2.h
      class Class2;
      class Class2 {
      //...
      }

      there won't have any recursive call problem anymore... i forgot to say, don't put the data members into public: statement... prefer this for the interface (member functions which are allowed for the users).


      TOXCCT >>> GEII power

      1 Reply Last reply
      0
      • I Iceberg76

        Hello, I am writing a application in Visual C++ .NET 7.0. I'd like to keep my classes in seperate files to keep things organized, but I am having difficulty accessing classes from within two files. Can someone help me find a solution accessing between files in this manner? I need to access methods and data back and forth in this manner, but I don't want the two classes to be in the same file. What am I doing wrong? Is there a different way to do this? Here is a simplified version: Class1.h #ifndef __Class1H__ #define __Class1H__ #include "Class2.h" class Class1 { public: Class1(); ~Class1(); //This doesn't work because it is in a different file! Class1(Class2* object); //This doesn't work because it is in a different file! Class2 obj; }; #endif // __Class1H__ Class2.h #ifndef __Class2H__ #define __Class2H__ //this is causing problems... #include "Class1.h" class Class2 { public: Class2(); ~Class2(); }; #endif // __Class2H__ Class1.cpp #include "Class1.h" Class1::Class1() { } Class1::~Class1() { } Class2.cpp #include "Class2.h" Class2::Class2() { } Class2::~Class2() { }

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        oh, i see another problem... each header file must be "included" once into the project. for that, you must use preprocessor directive to exclude recursive redifinitions :

        Class1.h
        #if !defined(__CLASS1_H_INCLUDED__)
        #define __CLASS1_H_INCLUDED__

        //...

        #endif//__CLASS1_H_INCLUDED__

        Class2.h
        #if !defined(__CLASS2_H_INCLUDED__)
        #define __CLASS2_H_INCLUDED__

        //...

        #endif//__CLASS2_H_INCLUDED__

        that concern only .h files. don't forget !


        TOXCCT >>> GEII power

        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