• 12
• 11
• 9
• 10
• 13

# class problems

This topic is 1799 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to create a basic class to go with my project, and I keep getting the error "unresolved external symbol." I have no idea what is causing it.

I use VC++ 2010 express

JEngine.h

#ifndef JENGINE_H
#define JENGINE_H

#include "stdafx.h"

class JEngine
{
public:
void Initialize();

};
#endif



JEngine.cpp

#include "JEngine.h"
#include "stdafx.h"

void JEngine::Initialize()
{
}



main.cpp

#include "stdafx.h"
#include "JEngine.h"

int main(int argc, char **argv){

JEngine* gameInstance = new JEngine();
gameInstance->Initialize();
return 1;

}


##### Share on other sites

JEngine.cpp

#include "JEngine.h"
#include "stdafx.h"


Should be

JEngine.cpp

#include "stdafx.h"
#include "JEngine.h"

?

?

##### Share on other sites

JEngine.cpp

#include "JEngine.h"
#include "stdafx.h"


Should be

JEngine.cpp

#include "stdafx.h"
#include "JEngine.h"

Eww, nice catch.  That is a nasty one to figure out.  But explaining what kicked ya in the butt is a good idea also...

What jelly caught is basically a problem with precompiled headers that will get you quite often.  A precompiled header is basically a memory dump of the symbol table at the point where the trigger file is accessed within the compile.  With "automatically generated by xxxx", the default in VC I believe, the symbol table dump is likely to be triggered at the start of the header for JEngine.h before the class is defined and included in the symbol table dump.  Reloading the symbol table overwrites anything which was already there and as such, in the cpp your class inclusion before the trigger include simply gets forgotten because the entire symbol table is loaded overwriting anything you may have already included.

This sounds confusing to me and I used to know it inside and out.  Just do as jelly mentions and it will fix it.  The details get pretty goofy. :)

##### Share on other sites

JEngine.cpp

#include "JEngine.h"
#include "stdafx.h"


Should be

JEngine.cpp

#include "stdafx.h"
#include "JEngine.h"

Eww, nice catch.  That is a nasty one to figure out.  But explaining what kicked ya in the butt is a good idea also...

What jelly caught is basically a problem with precompiled headers that will get you quite often.  A precompiled header is basically a memory dump of the symbol table at the point where the trigger file is accessed within the compile.  With "automatically generated by xxxx", the default in VC I believe, the symbol table dump is likely to be triggered at the start of the header for JEngine.h before the class is defined and included in the symbol table dump.  Reloading the symbol table overwrites anything which was already there and as such, in the cpp your class inclusion before the trigger include simply gets forgotten because the entire symbol table is loaded overwriting anything you may have already included.

This sounds confusing to me and I used to know it inside and out.  Just do as jelly mentions and it will fix it.  The details get pretty goofy.

This did not fix the problem... I am still confused!

##### Share on other sites

More on precompiled headers in VS...

#include "stdafx.h"

must be the first line of code in each cpp file, it ignores everything until it sees the precompiled header include.

What error are you now getting? A few guesses

1) It's moaning about wmain or WinMain -> wmain means you have compiled the program using the Unicode char set instead of ANSI charset, this is in the options for the project. If it moans about WinMain, you didn't make a console project, make a new project that is a console app.

2) It worked, but after running it said there was an error. Don't return 1 from main, return 0 instead.

Those are just guesses though, please post the errors you are getting, not just "it doesn't work".

EDIT: Don't #include "stdafx.h" in header files either, that could be your problem...

##### Share on other sites

Good clarifications Para, I remember the problems but not the specifics anymore.  I just don't use precompiled headers anymore and maintain low compile times without them.  :)  Of course doing that requires being an anal retentive prick about header design. :)

##### Share on other sites

Those are just guesses though, please post the errors you are getting, not just "it doesn't work".

Error 2 error LNK2019: unresolved external symbol "public: void __thiscall JEngine::Initialize(void)" (?Initialize@JEngine@@QAEXXZ) referenced in function _main C:\Users\Seth Hope\Documents\Visual Studio 2010\Projects\JcamEngine2\JcamEngine2\Jengine2.obj JcamEngine2

That's the error I get.

 Here is the modified code. It still throws that error.

JEngine.cpp
#include "stdafx.h"
#include "JEngine.h"

void JEngine::Initialize()
{
}


JEngine.h

#ifndef JENGINE_H
#define JENGINE_H

class JEngine
{
public:
void Initialize();

};
#endif



main.cpp


#include "stdafx.h"
#include "JEngine.h"
}

int main(int argc, char **argv){

JEngine* gameInstance = new JEngine();
gameInstance->Initialize();

return 0;

}


Edited by sethhope

##### Share on other sites

Did you included JEngine.cpp in your project or in build script (in whatever development environment you are using)?

##### Share on other sites

Try cleaning and doing a full rebuild. Do you really need the precompiled headers?

Edited by Khatharr

##### Share on other sites

In visual studio, in the Solution Explorer, expand the option marked "Source Files" and see if JEngine.cpp is in it. If not, right click "Source Files" and go to Add -> Existing Item and find JEngine.cpp with the file explorer. Build and run.

Edited by Shaquil