Linker Error! Help!

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

Linker Error! Help!

Post 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__

User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Linker Error! Help!

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

Re: Linker Error! Help!

Post 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!!!!!! :)
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Linker Error! Help!

Post by Falco Girgis »

Why the hell isn't this in programming discussion?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Linker Error! Help!

Post by dandymcgee »

GyroVorbis wrote:Why the hell isn't this in programming discussion?
Moved. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply