Page 1 of 1

Linker Error! Help!

Posted: Sun Sep 18, 2011 3:32 am
by Pornomag
Okay so after some busy coding I find myself with a linker error, that I just can't seem to get rid of.. I have checked throughout the files to see if I forgot to do anything, but everything seems ok, but apparently not according to Visual Studio... Visual Studio is complaining that ge::win::BaseWindow* RenderWindow has been already defined in NCMainMenu.obj, and it's complaining in the main.obj. Here's my source code, I could put up the whole project if you can help me fix it :L.

This is the source file that it is complaining about:
main.cpp

Code: Select all

#include <iostream>
using std::cout;

#include <vector>

// To initialize SDL
#include "InitSDLManager.h"


// For the Game-Scenes
#include "Scene.h"

// The Main Menu Scene
#include "NCMainMenu.h"

// For images
#include "SWRImage2D.h"

// For the Window
#include "MiguelsGameEngine.h"
#include "SWRWindow.h"

int main(int argc, char* argv[])
{
	// Create an InitShutdownManager to automatically call SDL_Quit()
	// when it goes out of scope.
	ge::util::InitSDLManager SDLAutoInitalizer;

	// Initialize SDL
	SDLAutoInitalizer.Init(SDL_INIT_EVERYTHING);
	SDLAutoInitalizer.InitSound();
	SDLAutoInitalizer.InitTTF();

	// Make the RenderWindow a Software rendering window.
	RenderWindow = &ge::win::SWRWindow(800, 600, 32, SDL_DOUBLEBUF);
	if(RenderWindow->Create() == false){
		return -1;
	}
	
	ge::img::SWRImage2D icon;
	if(icon.LoadImage("resources/icons/Nyan Cat.bmp") == false){
		return -4;
	}

	SDL_WM_SetIcon(icon.getImageSurface(), 0);

	std::vector<ge::util::Scene*> gameScenes;

	gameScenes.push_back(&NCMainMenu());

	int SceneNum = 0;

	while((SceneNum = gameScenes.at(0)->MainLoop()) >= 0)
	{
		// Get rid of whatever is in the array..
		gameScenes.pop_back();

		switch(SceneNum){
		case 0:
			gameScenes.push_back(&NCMainMenu());
			break;
		default:
			return SceneNum;
		}
		
	}


	return SceneNum;
}
It says ge::win::BaseWindow* RenderWindow has already been defined.
NCMainMenu.h

Code: Select all

/************************************************************************
* NCMainMenu.h
*
* This is the Main Menu for the game, I am creating called Nyan Cat The Game.
* It will have the ability to travel throughout the game.
* 
* In the main menu you can:
*	1. Play
*	2. How To Play (help menu)
*	3. Credits
*
*											Author: Miguel Martin (c) 2011. 
*************************************************************************/

#pragma once
#ifndef __NCMAINMENU_H__
#define __NCMAINMENU_H__

#include "Scene.h"

class NCMainMenu : public ge::util::Scene
{
public:
	// Main function for the main loop
	virtual int MainLoop();

	// Initializes variables
	virtual int Init();

	// Handles user input
	virtual void HandleInput(SDL_Event&);
};

#endif // __NCMAINMENU_H__
NCMainMenu.cpp

Code: Select all

#include "NCMainMenu.h"

#include "FPSCounter.h"

#include "MiguelsGameEngine.h"

int NCMainMenu::MainLoop()
{
	const ge::util::FPSCounter FPS(20); 
	SDL_WM_SetCaption("Main Menu!", 0);


	SDL_Event event;

	while( !isDone() ){
		while(SDL_PollEvent(&event)){
			// Handle the Input from the user
			HandleInput(event);
		}
		
		// Limit the Frame-Rate.
		FPS.LimitFPS();
	}

	return 0;
}

int NCMainMenu::Init()
{

	return 0;
}

void NCMainMenu::HandleInput(SDL_Event& event)
{
	RenderWindow->HandleInput(event);
}

and this is where RenderWindow is actually located at
MiguelsGameEngine.h

Code: Select all

/************************************************************************
* MiguelsGameEngine.h
* 
* This header file declares a global BaseWindow null pointer. Used to make a
* generic Window to render your graphics.
* 
* 										Author: Miguel Martin. (c) 2011
************************************************************************/

#pragma once
#ifndef __MIGUELSGE_H__
#define __MIGUELSGE_H__

#include "BaseWindow.h"


// The Rendering Window. Use this to Render onto the screen.
// If you would like both Software Rendering and OpenGL rendering abilities.
ge::win::BaseWindow* RenderWindow = 0;

#endif // __MIGUELSGE_H__


Re: Linker Error! Help!

Posted: Sun Sep 18, 2011 3:38 am
by Ginto8
You're defining RenderWindow in 2 different source files. If you want to prevent this, replace the header file declaration/definition combination with:

Code: Select all

extern ge::win::BaseWindow* RenderWindow;
and then in a single source file, put

Code: Select all

ge::win::BaseWindow* RenderWindow = 0;
This keeps the compiler being confused about which source file's definition of the variable is correct.

Re: Linker Error! Help!

Posted: Sun Sep 18, 2011 4:18 am
by Pornomag
Ginto8 wrote:You're defining RenderWindow in 2 different source files. If you want to prevent this, replace the header file declaration/definition combination with:

Code: Select all

extern ge::win::BaseWindow* RenderWindow;
and then in a single source file, put

Code: Select all

ge::win::BaseWindow* RenderWindow = 0;
This keeps the compiler being confused about which source file's definition of the variable is correct.
Thank you very much!!!!!! :)

Re: Linker Error! Help!

Posted: Sun Sep 18, 2011 1:26 pm
by Falco Girgis
Why the hell isn't this in programming discussion?

Re: Linker Error! Help!

Posted: Sun Sep 18, 2011 2:17 pm
by dandymcgee
GyroVorbis wrote:Why the hell isn't this in programming discussion?
Moved. ;)