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

  • Context: C/C++ 
  • Thread starter Thread starter darkchild
  • Start date Start date
  • Tags Tags
    File
Click For Summary

Discussion Overview

The discussion revolves around a problem with a C++ project where a .cpp file fails to recognize a corresponding .h header file, leading to compilation errors. Participants explore potential causes and solutions related to file structure, include directives, and class declarations.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes having split a class between a header file and a .cpp file, encountering errors indicating the class has not been declared despite using the include directive.
  • Another participant requests a simplified version of the code to better understand the issue and suggests creating a "toy" program to demonstrate the error.
  • A participant shares a minimal example of the class and its implementation, which still produces the same errors, indicating a fundamental issue.
  • One participant speculates that the error might stem from the compiler including an unexpected version of the header file, suggesting the possibility of multiple files with the same name in the project directory.
  • Another participant points out a potential mistake in the header file's preprocessor directives, suggesting that the order of `#define` and `#ifndef` might be incorrect.

Areas of Agreement / Disagreement

Participants express differing views on the source of the problem, with some suggesting issues related to file inclusion and others pointing to potential mistakes in the header file's syntax. No consensus is reached on the exact cause of the errors.

Contextual Notes

There are indications of possible confusion regarding file locations and naming conventions, as well as the correct usage of preprocessor directives in the header file. These factors remain unresolved in the discussion.

darkchild
Messages
153
Reaction score
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
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.
 
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:
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.
 
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 ;)
 

Similar threads

  • · Replies 0 ·
Replies
0
Views
3K
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
81
Views
8K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 13 ·
Replies
13
Views
1K