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. Access to class in another file.

Access to class in another file.

Scheduled Pinned Locked Moved C / C++ / MFC
c++csharphelpquestionannouncement
8 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 another file. Can someone help me find a solution accessing between files? I would rather not have multiple classes in a single file. What am I doing wrong? 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__ 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() { }

    M I 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 another file. Can someone help me find a solution accessing between files? I would rather not have multiple classes in a single file. What am I doing wrong? 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__ 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() { }

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #2

      I don't see any problem. VC++ 7 is quite happy with those four files. Maxwell Chen

      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 another file. Can someone help me find a solution accessing between files? I would rather not have multiple classes in a single file. What am I doing wrong? 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__ 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() { }

        I Offline
        I Offline
        Iceberg76
        wrote on last edited by
        #3

        Oops, well it appears that my simplified sample compiles. I was able to make the problem I was having go away, by declaring the class at the top of the Class1.h file: include... class Class2; class Class1; Even though it works for my simplified version, is there a better or correct way to do this?

        M 1 Reply Last reply
        0
        • I Iceberg76

          Oops, well it appears that my simplified sample compiles. I was able to make the problem I was having go away, by declaring the class at the top of the Class1.h file: include... class Class2; class Class1; Even though it works for my simplified version, is there a better or correct way to do this?

          M Offline
          M Offline
          Maxwell Chen
          wrote on last edited by
          #4

          Well, I encourage you to read Chapter 9 : "Source Files and Programs" of the book "The C++ Programming Language, 3rd Edition" by Stroustrup. Maxwell Chen

          I 2 Replies Last reply
          0
          • M Maxwell Chen

            Well, I encourage you to read Chapter 9 : "Source Files and Programs" of the book "The C++ Programming Language, 3rd Edition" by Stroustrup. Maxwell Chen

            I Offline
            I Offline
            Iceberg76
            wrote on last edited by
            #5

            Good advice, I will get the book. thanks

            1 Reply Last reply
            0
            • M Maxwell Chen

              Well, I encourage you to read Chapter 9 : "Source Files and Programs" of the book "The C++ Programming Language, 3rd Edition" by Stroustrup. Maxwell Chen

              I Offline
              I Offline
              Iceberg76
              wrote on last edited by
              #6

              Hello, I figured out the problem I was having, but I still don't know a good solution. If you take the code posted above... and add the following line to Class2.h: #include "Class1.h" So there seems to be a problem at the posted comments when I add this include. Why can't I link in this way, and what suggestions can anybody give me to avoid this sort of thing. Thank You

              M 1 Reply Last reply
              0
              • I Iceberg76

                Hello, I figured out the problem I was having, but I still don't know a good solution. If you take the code posted above... and add the following line to Class2.h: #include "Class1.h" So there seems to be a problem at the posted comments when I add this include. Why can't I link in this way, and what suggestions can anybody give me to avoid this sort of thing. Thank You

                M Offline
                M Offline
                Maxwell Chen
                wrote on last edited by
                #7

                Originally Class2 has been included by Class1 in file "Class1.H". Now you added #include "Class1.h" in file Class2.h, you got cross inclusion! ... like your bowels getting knotted. #include "some_file" means to embed the entire texts of the file "some_file" into where the #include directive being invoked. Foe example, "MyHeader.H":

                class Foo { };

                And another file, FooBar.h, includes MyHeader:

                #include "MyHeader.h"
                class Bar {
                public:
                Bar() { }
                };

                You can now imagine file "FooBar.h" as the below:

                class Foo { };
                class Bar {
                public:
                Bar() { }
                };

                And to the view of compiler, it is! Maxwell Chen

                I 1 Reply Last reply
                0
                • M Maxwell Chen

                  Originally Class2 has been included by Class1 in file "Class1.H". Now you added #include "Class1.h" in file Class2.h, you got cross inclusion! ... like your bowels getting knotted. #include "some_file" means to embed the entire texts of the file "some_file" into where the #include directive being invoked. Foe example, "MyHeader.H":

                  class Foo { };

                  And another file, FooBar.h, includes MyHeader:

                  #include "MyHeader.h"
                  class Bar {
                  public:
                  Bar() { }
                  };

                  You can now imagine file "FooBar.h" as the below:

                  class Foo { };
                  class Bar {
                  public:
                  Bar() { }
                  };

                  And to the view of compiler, it is! Maxwell Chen

                  I Offline
                  I Offline
                  Iceberg76
                  wrote on last edited by
                  #8

                  Thanks, No more cross-inclusion.

                  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