Sign in to follow this  
Jesse Dager

class problems

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 this post


Link to post
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 this post


Link to post
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. smile.png

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

Share this post


Link to post
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...

Edited by Paradigm Shifter

Share this post


Link to post
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 this post


Link to post
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.
 
[edit] 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 this post


Link to post
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

Share this post


Link to post
Share on other sites

How come you don't delete gameInstance?

 

The page it exists in will be freed when the application terminates. It's good form to do it anyway, but on most platforms it's not necessary in a case like this.

 

The real question is why it's dynamically allocated in the first place.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this