class problems

Started by
10 comments, last by Khatharr 11 years ago

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;
	
}

I develop to expand the universe. "Live long and code strong!" - Delta_Echo (dream.in.code)
Advertisement

JEngine.cpp


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

Should be

JEngine.cpp


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

?

?



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. :)

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!

I develop to expand the universe. "Live long and code strong!" - Delta_Echo (dream.in.code)

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...

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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. :)

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;
	
}

I develop to expand the universe. "Live long and code strong!" - Delta_Echo (dream.in.code)

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

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

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

This topic is closed to new replies.

Advertisement