Problem Creating a separate file for a class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Problem Creating a separate file for a class

I am testing my c++ knowledge in a console application call test, the thing is that when I create a separate class,I use it in main and I run it, the compiler say that there's not such file so I can't use it. I already include the class with #include and I belive that I have the constructor and the destructor functions (the only two that the class have for now) right Here is the message that the compilers sais when I try to run the application "No such file or directory" I know it is the class because before creating it as a separate file the application run fine Here is the code: Main.cpp #include <iostream> #include <string> #include <Class.h> using namespace std; int main() { Class obj; return 0; } Class.h #ifndef CLASS_H #define CLASS_H class Class { public: Class(); ~Class(); protected: private: }; #endif // CLASS_H Class.cpp #include "Class.h" Class::Class() { cout << "Hi" << endl; } Class::~Class() { cout << "Bye" << endl; } I am doing this on my computer with code blocks and the compiler gnu

23rd Apr 2017, 8:02 PM
Luis Alfonso Alvarez
Luis Alfonso Alvarez - avatar
10 Answers
+ 11
what compiler do you have?
22nd Apr 2017, 12:51 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 10
If you are using the gnu compiler do the following: g++ -include=Class.cpp Main.cpp I forgot if you should use the '=' sign between the include and the class. Try it with it if it do not work. See the help about g++ on include flag. hope it helps.
21st Apr 2017, 2:13 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 10
Try this: . Add '#include "Class.cpp" ' to the end of Class.h, just before #endif. . Make sure all the files are in the same directory. After doing this, tell us if it worked.
23rd Apr 2017, 9:38 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 7
use #include "class.h" instead of #include <class.h>
21st Apr 2017, 2:33 AM
jay
jay - avatar
+ 5
are you trying to this on a computer or on codeplayground?
22nd Apr 2017, 2:26 AM
jay
jay - avatar
+ 5
It should look something like this in main.cpp #include "Class.h"
5th May 2017, 4:11 AM
jay
jay - avatar
+ 4
@Luis there are a many users who would like to help you with your code. They can help, if you tap "insert" when you make a new comment or question to post your code.
21st Apr 2017, 1:13 AM
Manual
Manual - avatar
+ 2
I am using a computer with the code blocks, with the compiler gnu
23rd Apr 2017, 8:02 PM
Luis Alfonso Alvarez
Luis Alfonso Alvarez - avatar
+ 1
I still need help, the solutions that the past answers gave me didn't work for me
22nd Apr 2017, 12:13 AM
Luis Alfonso Alvarez
Luis Alfonso Alvarez - avatar
0
Ulisses it didn't work
5th May 2017, 4:03 AM
Luis Alfonso Alvarez
Luis Alfonso Alvarez - avatar