Access to class in another file.
-
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() { }
-
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 don't see any problem. VC++ 7 is quite happy with those four files. Maxwell Chen
-
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() { }
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?
-
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?
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
-
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
-
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
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
-
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
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
-
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