Error is driving me nuts

Started by
38 comments, last by Camilos 9 years, 5 months ago

didn't work, but I think i'm going to change gameState to a string var, see if that works

Advertisement

ok, i've tried changing it to a string variable and then i get this error:

>game.obj : error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Game::gameState" (?gameState@Game@@2V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)

and i don't really understand it

here are the code


class Game{
public: 
	static void Start();
	static std::string gameState;
	Game();
	~Game();

private:
	static bool IsExiting();
	static void GameLoop();
  
 
};

#include "basic.h"
#include "game.h"
#include <string>


Game::Game(){
	gameState = "Uninitialized";
};
Game::~Game(){

};

void Game::Start(){
	if(gameState != "Uninitialized"){
		return;
	};
	gameState= "Playing";
	
};
bool Game::IsExiting(){
	if(gameState == "Exiting"){ 
		return true;
	}
	else{ 
		return false;
	};

}
void Game::GameLoop(){
	sf::Event Event;
	sf::RenderWindow mainWindow(sf::VideoMode(1024,768,32),"Pong");
		while(mainWindow.pollEvent(Event)){
			if(gameState == "Playing"){
				mainWindow.clear(sf::Color::Black);
				mainWindow.display();

				if((Event.type == sf::Event::Closed)||(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))){
					gameState = "Exiting";
				};
			}
		};

};

When you say "didn't work", are you referring to my code? If so, can you explain what didn't work? Compilation error? Linker error? Runtime error? Something else?

As for your error, that means the linker cannot find a definition of "gameState".

When you have a static variable declaration in a class, you must also provide a definition in exactly one source file:
std::string Game::gameState = "Uninitialized";
Also note that using a constructor and destructor here might not do what you want as up until now you've not been using any Game class instances, so the constructor may not run.

What i meant was that the same thing happend as before, when the console screen appeared, an error message appeared saying '"Unhandled exception at 0x77cad062 in Pong.exe: 0xC0000005: Access violation writing location 0x00000004"

think I was wrong and that the problem lays with sf::RenderWindow Game::MainWindow;

because i've changed the code and it worked(kind of, not like I wanted) but when i put this line (sf::RenderWindow Game::MainWindow;) it showed the error.

Edit:

I think i've found it, ive found this link http://stackoverflow.com/questions/24318663/displaying-window-with-sfml and when i removed the static, the error vanished,

Can you show us the code that worked? A simpler example is better, so if you can get it working in one file that would be easier to help you with. If not, please post all the code.

IT'S ALIVE!!!!

I finally completed step 2 from http://www.gamefromscratch.com/page/Game-from-Scratch-CPP-Edition-Part-2.aspx after two days of searching biggrin.png

here is the now correct code for people who experienced similar problems:


#include <SFML\Graphics.hpp>
#include <SFML\Window.hpp>
#include <SFML\Audio.hpp>
#include <SFML\System.hpp>

#include <iostream>
#include <ctime>
#include <stdio.h>
#include <map>
#include <cassert>
#include <string>

game.h:


#include "basic.h"

class Game{
public: 
	void Start();
	
	
	

private:
	
	 void GameLoop();
	 bool IsExiting();
	enum GameState { Uninitialized, ShowingSplash, Paused, 
          ShowingMenu, Playing, Exiting };
  
	GameState gameState;
 sf::RenderWindow mainWindow;
};

Game.cpp:


#include "basic.h"
#include "game.h"
#include <string>


void Game::Start(){
	gameState= Game::Playing;
	mainWindow.create(sf::VideoMode(1027,768,32),"Pong");
	while(!IsExiting()){
		GameLoop();
	}
	mainWindow.close();
	
};

bool Game::IsExiting(){
	if(gameState == Game::Exiting){
		return true;}

	else{ return false;};

}

void Game::GameLoop(){
	sf::Event Event;
			while(mainWindow.pollEvent(Event)){
				switch(gameState){
					case Game::Playing:
			
				mainWindow.clear(sf::Color::Black);
				mainWindow.display();

				if((Event.type == sf::Event::Closed)||(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))){
					gameState = Game::Exiting;
				};
				break;
			
		};

};
};

and main.cpp


#include "basic.h"
#include "game.h"

int main(){
	Game Pong;
	Pong.Start();
	

	return 0;

};

God i'm glad that it works biggrin.png

Great.

Many would argue that this is a superior design to using static variables anyway. Global variables like that make it much easier to write code that is hard to understand and change.

And i'm stuck again :(

got up to part 5 pretty easy but know i've got an error and i don't know why:


PlayerPaddle *player1 = new PlayerPaddle();
	player1->Load("images/paddle.png");
	player1->SetPosition((1024/2)-45,700);
	gameObjectManager.Add("Paddle1",player1);

In the last line, it says that the type of Playerpaddle* is incompatible with parameter of type VisibleGameObject* but Playerpaddle is a class derived from VisibleGameObject so why doesn't he accept it?

posting the output of the debugger in case it would help

1>------ Build started: Project: Pong 2.0, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\visiblegameobject.h(4): error C2011: 'VisibleGameObject' : 'class' type redefinition
1> c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\visiblegameobject.h(4) : see declaration of 'VisibleGameObject'
1>c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\gameobjectmanager.h(23): warning C4150: deletion of pointer to incomplete type 'VisibleGameObject'; no destructor called
1> c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\visiblegameobject.h(4) : see declaration of 'VisibleGameObject'
1> game.cpp
1>c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\visiblegameobject.h(4): error C2011: 'VisibleGameObject' : 'class' type redefinition
1> c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\visiblegameobject.h(4) : see declaration of 'VisibleGameObject'
1>c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\gameobjectmanager.h(23): warning C4150: deletion of pointer to incomplete type 'VisibleGameObject'; no destructor called
1> c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\visiblegameobject.h(4) : see declaration of 'VisibleGameObject'
1>c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\game.cpp(12): error C2039: 'Load' : is not a member of 'PlayerPaddle'
1> c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\playerpaddle.h(3) : see declaration of 'PlayerPaddle'
1>c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\game.cpp(13): error C2039: 'SetPosition' : is not a member of 'PlayerPaddle'
1> c:\users\ruben\documents\visual studio 2010\projects\pong 2.0\pong 2.0\playerpaddle.h(3) : see declaration of 'PlayerPaddle'
1> Generating Code...
1> Compiling...
1> PlayerPaddle.cpp
1> Generating Code...

This topic is closed to new replies.

Advertisement