I'm trying to make a character which moves around the screen in C++ using the SFML library. Every time a make a project i end up with the same LNK 2001 error.
Please explain to me what I've done wrong and how do i fix it. I'm still a beginner so if you have any recommendations about fixing errors or writing code I'd love it.
Main.cpp:
#include "Game.cpp"
#include "Game.h"
int main ()
{
Game Game;
Game.GameRun();
}
Game.h:
#pragma once
#include <SFML\Window.hpp>
#include "Character.h"
class Game
{
public:
void GameRun ();
void UpdateGame(Character player);
void Controls(int sourceX, int sourceY, Character player);
};
Character.h:
#pragma once
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include "Game.h"
class Character
{
public:
enum Direction {Down, Left, Right, Up};
int characterX;
int characterY;
sf::Texture txt_character;
sf::Sprite spr_character;
};
Game.cpp:
#include "Game.h"
#include "Character.h"
#include <iostream>
inline void Game::GameRun()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML test");
Character player;
player.characterX = 1;
player.characterY = Character::Down;
player.txt_character.loadFromFile("character sprite sheet.png");
player.spr_character.setTexture(player.txt_character);
while(window.isOpen())
{
Game::UpdateGame(player);
}
}
inline void Game::UpdateGame(Character player)
{
sf::Event event;
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML test");
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
Game::Controls(player.characterX, player.characterY, player);
player.spr_character.setTextureRect(sf::IntRect(player.characterX * 32, player.characterY * 32, 32, 32));
window.draw(player.spr_character);
window.display();
window.clear();
}
inline void Game::Controls(int sourceX, int sourceY, Character player)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
sourceY = Character::Up;
sourceX++;
if (sourceX * 32 >= player.txt_character.getSize().x)
sourceX = 0;
player.spr_character.move(0, -2);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
sourceY = Character::Down;
sourceX++;
if (sourceX * 32 >= player.txt_character.getSize().x)
sourceX = 0;
player.spr_character.move(0, 2);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
sourceY = Character::Left;
sourceX++;
if (sourceX * 32 >= player.txt_character.getSize().x)
sourceX = 0;
player.spr_character.move(-2, 0);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
sourceY = Character::Right;
sourceX++;
if (sourceX * 32 >= player.txt_character.getSize().x)
sourceX = 0;
player.spr_character.move(2, 0);
}
}
Errors:
1>main.obj : error LNK2001: unresolved external symbol "public: static int Character::characterY" (?characterY@Character@@2HA)
1>main.obj : error LNK2001: unresolved external symbol "public: static int Character::characterX" (?characterX@Character@@2HA)
1>C:\Users\SONY\Documents\Visual Studio 2010\Projects\Top Down Game\Debug\Game.exe : fatal error LNK1120: 2 unresolved externals
Thanks :D
EDIT: FYI i had the code without classes and it was working perfectly, it was just a big block of code in the main.cpp
EDIT: link to folder for program: https://www.dropbox.com/s/3yyjti8zwu019s7/Top%20Down%20Game%20With%20Classes.rar