954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Classes in separate files

I just watched this tutorial:

http://www.youtube.com/watch?v=NTip15BHVZc

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:

#ifndef TESTCLASSFILE_H
#define TESTCLASSFILE_H


class testClassFile
{
    public:
        testClassFile();
};

#endif // TESTCLASSFILE_H


src\testClassFile.cpp:

#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";
}


I am using Code::Blocks.

Thanks.

ben1996123
Junior Poster in Training
82 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 
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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
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.

ben1996123
Junior Poster in Training
82 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

The file is in the project, but it can't find it.

[img]http://img690.imageshack.us/img690/8673/codeblocks.png[/img]

ben1996123
Junior Poster in Training
82 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Worked fine for me. Attached is a zipped file. Unzip and open the cbp file with Code Blocks and see if it works for you.

Attachments ClassLink.zip (2.3KB)
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
Worked fine for me. Attached is a zipped file. Unzip and open the cbp file with Code Blocks and see if it works for you.

Ah that works, it appears that the files were not in the correct directory of the one I made. Thanks :D

ben1996123
Junior Poster in Training
82 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: