Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error with cpp file

Status
Not open for further replies.

timmay3141

Programmer
Joined
Dec 3, 2002
Messages
468
Location
US
I have a class that I added to the workspace by creating the header file and cpp file on my own (as opposed to going to Insert...New Class). Here's what the header file is like, in short:

#if !defined(_MYCLASS_H_)
#define _MYCLASS_H_

class MyClass
{
// Stuff in here
};

#include "MyClass.cpp"
#endif

The cpp file is just definitions, with no declarations or preprocessor directives other than #include "MyClass.h". I get this error message:

fatal error C1010: unexpected end of file while looking for precompiled header directive

What is causing this? I did stuff like this all the time with a Borland compiler last year, and I never had any problems. Any ideas?
 
you need to include the precompiled header, most likely stdafx.h

Matt
 
Okay, I #include "stdafx.h" and now I get this error:

error C2084: function '__thiscall GlobalProperties::GlobalProperties(void)' already has a body

I certainly have not given it a body before, yet the compiler thinks I have. I did a Find in Files search and it didn't find that I had any other definitions, either. Any ideas?
 
I sometimes get a similar error message when I haven't pressed return after typing a line. Like:

#endif | <-cursor

when you want:

#endif
| <-cursor

Try that.
 
Nope, that didn't fix it. Thanks for the suggestion though. Any other ideas?
 
in ur original post what is this:

#include &quot;MyClass.cpp&quot;

i've never seen that before.

-pete
 
Well, in my old compiler you needed to #include the cpp file at the end of the cooresponding header file. I noticed it doesn't work that way in VC++, so maybe I should #include it somewhere else...I suppose that could be the problem.
 
>> so maybe I should #include it somewhere else

if the .cpp file is part of the project it is in the build. then in the .cpp file you include the precompiler header and the .cpp file header like this

#include &quot;stdafx.h&quot;
#include &quot;myclass.h&quot;

hope that helps
-pete
 
You should never include a .cpp file. That never makes sense.

The only time it even remotely makes sense is when it contains definitions for template functions, and in that case, it's not really a .cpp file, anyway.
 
correct... the only application of it that I have seen is in template classes. Make sure you add the .cpp files to the project. You can do this by clicking the &quot;File&quot; tab in the workspace. Right click on the source folder and add files. Same with the header files. My guess is that the cpp file was not part of the project.

Matt
 
Wow...I feel like an idiot. You HAD to include the cpps in my old compiler. I guess it's good that adding it to the workspace will make the compiler use it automatically, but it had never occurred to me. It works now. Thanks for making me aware of this, everyone.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top