and tried it myself, but I could not get it to work, I get these errors:
||=== 15. Placing classes in separate files, Debug ===|
C:\Program Files (x86)\CodeBlocks\programs\tutorials\15. Placing classes in separate files\src\testClassFile.cpp|1|error: testClassFile.h: No such file or directory|
C:\Program Files (x86)\CodeBlocks\programs\tutorials\15. Placing classes in separate files\src\testClassFile.cpp|5|error: 'testClassFile' has not been declared|
C:\Program Files (x86)\CodeBlocks\programs\tutorials\15. Placing classes in separate files\src\testClassFile.cpp|5|error: ISO C++ forbids declaration of 'testClassFile' with no type|
C:\Program Files (x86)\CodeBlocks\programs\tutorials\15. Placing classes in separate files\src\testClassFile.cpp||In function 'int testClassFile()':|
C:\Program Files (x86)\CodeBlocks\programs\tutorials\15. Placing classes in separate files\src\testClassFile.cpp|8|warning: no return statement in function returning non-void|
||=== Build finished: 3 errors, 1 warnings ===|
Here is the code from each of the 3 files:
main.cpp:
#include <iostream>
#include "testClassFile.h" //include the header file
using namespace std;
int main()
{
testClassFile test;
return 0;
}
// To create a new file, go to file > new > class
// Name the class, deselect has destructor/virtual destructor, select header/implementation file shall be in the same folder
// Click yes etc.
// It creates a <class>.h and <class>.cpp file
// .h file - type all variables and function prototypes
// .cpp file - main code, function
// #include the required stuff in the .cpp file
#include "testClassFile.h"
#include <iostream>
using namespace std;
testClassFile::testClassFile() //the first testClassFile = the class name, the second is the function name (this one is a constructor). x::y() means y is a member of class x
{
cout << "this is text in a constructor";
}
C:\Program Files (x86)\CodeBlocks\programs\tutorials\15. Placing classes in separate files\src\testClassFile.cpp|1|error: testClassFile.h: No such file or directory|
It can't find this file, so it can't compile and link. You need to add that file to the project in Code Blocks. Right click on the project and click "Add Files", then maneuver to the project and select it. You might have to select the configuration too. Code Blocks will guide you. Then clean the project and build it and see if it works.
It can't find this file, so it can't compile and link. You need to add that file to the project in Code Blocks. Right click on the project and click "Add Files", then maneuver to the project and select it. You might have to select the configuration too. Code Blocks will guide you. Then clean the project and build it and see if it works.
Did this, it didn't work.
I opened the file, right clicked it and went to click Add file to current project, but all it said was Remove file from project. It is already in the project, but it still doesn't work, I just get the same errors.