Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Mar 20, 2009 #1
    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.
     
  2. jcsd
  3. Mar 20, 2009 #2

    jtbell

    User Avatar

    Staff: Mentor

    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.
     
  4. Mar 20, 2009 #3
    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: Mar 20, 2009
  5. Mar 20, 2009 #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.
     
  6. Mar 20, 2009 #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 ;)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++: .cpp file won't recognize .h file
Loading...