C++: .cpp file won't recognize .h file

  • C/C++
  • Thread starter darkchild
  • Start date
  • Tags
    File
In summary, the conversation discusses an issue with a class that has been split into a header file and a .cpp file, resulting in errors when building the project. The person suggests simplifying the code or creating a "toy" program to demonstrate the error. However, upon further inspection, it is discovered that the header file is incorrect and the person suggests fixing it.
  • #1
darkchild
155
0
I have a class that I've split over a header file (class declaration), and a .cpp file (member function definitions). Building the entire project results in various errors that say that the class has not been declared.

I've put in the include directive for the header file. Both files are definitely a part of the same project. I'm using the scope resolution operator with the class name for the member function definitions in the .cpp file. I have the same issue whether I use Dev-C++ or Visual Studios. I have no idea what the problem could be.
 
Technology news on Phys.org
  • #2
It's kind of hard to say anything specific without seeing the code. Can you strip your code down to a small subset (unless your program is small to begin with) that demonstrates the error when you compile it, and post it here? Also tell us exactly where each file is located in your folder structure.

Or maybe you can write a simple "toy" program that doesn't do anything except demonstrate the error, so we don't have to wade through a lot of irrelevant detail.
 
  • #3
jtbell said:
Or maybe you can write a simple "toy" program that doesn't do anything except demonstrate the error, so we don't have to wade through a lot of irrelevant detail.

Great idea. It looks like the problem is far more fundamental because the following worthless class also generates the error:

.cpp file:

#include "testclass.h"
using namespace std;

void testclass::doStuff()
{
;
}

header file:

#define TESTCLASS_H
#ifndef TESTCLASS_H

using namespace std;

class testclass {

public:
void doStuff();

private:
float yourBoat;
};

#endif

I get these errors with my class and this test class in Dev-C++:
7 C:\Dev-Cpp\testclass.cpp `testclass' has not been declared
C:\Dev-Cpp\Makefile.win [Build Error] [testclass.o] Error 1
 
Last edited:
  • #4
hmm, strange. I can't see anything wrong. I suspect the error is due to something else.
One possibility is that the testclass.h the compiler picks is actually not the one you expect.
Do you have a testclass.h (maybe an empty file) somewhere that gets included instead of the one you think?

Remember that in the IDE, the h files of the project are not being compiled, they are just there for convenience. The compiler picks the first testclass.h it finds according to the rules
of #include.
 
  • #5
oh, no, now that i look again. Your H file is wrong.

You have the #define TESTCLASS_H #ifndef TESTCLASS_H mixed up.
Switch them ;)
 

FAQ: C++: .cpp file won't recognize .h file

1. Why is my .cpp file not recognizing my .h file?

This could be due to several reasons, such as incorrect file paths, missing or corrupted header files, or errors in the code itself. Check for any typos in the file paths and make sure all necessary header files are included in the project. If the issue persists, carefully review the code for any errors or consult with a more experienced programmer for assistance.

2. How do I fix the issue of my .cpp file not recognizing my .h file?

First, try to identify the root cause of the issue by checking for any errors in the code or missing header files. If the issue persists, try cleaning and rebuilding the project, as this can sometimes resolve any linking issues. If the problem still persists, seek help from a more experienced programmer or consult online resources for troubleshooting steps.

3. Can I include multiple .h files in my .cpp file?

Yes, you can include multiple .h files in your .cpp file. This is especially useful when working on larger projects that require multiple header files for different functionalities. However, make sure to avoid duplicate definitions or declarations to prevent any conflicts.

4. Do I need to include all .h files in my .cpp file?

No, you only need to include the necessary .h files in your .cpp file. Including unnecessary header files can slow down the compilation process and may cause conflicts in the code. It is best to only include the header files that are directly referenced in your .cpp file.

5. Can I use forward declaration to solve the issue of my .cpp file not recognizing my .h file?

Yes, forward declaration can be used to solve the issue of a .cpp file not recognizing a .h file. This involves declaring the class or function in the .cpp file before it is referenced, thus eliminating the need for a header file. However, this approach can be more complex and is not always recommended. It is best to properly include the necessary header files for better code organization and readability.

Similar threads

Back
Top