Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] XCode wont let me compile if I put this Game class in its own file  (Read 2504 times)

0 Members and 1 Guest are viewing this topic.

Erkoren

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Hi!

If I try to run this code from the SFML Game Development Book. I can run it if the class and the main() are in the same file but once I put the class in its own header file, which I include in the main file XCode won't let me compile. I'm on Mac OSX 10.10.

#include <SFML/Graphics.hpp>
class Game
{
public:
  Game();
  void run();
private:
  void processEvents();
  void update();
  void render();
private:
  sf::RenderWindow mWindow;
  sf::CircleShape  mPlayer;
};

Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application") , mPlayer()
{
  mPlayer.setRadius(40.f);
  mPlayer.setPosition(100.f, 100.f);
  mPlayer.setFillColor(sf::Color::Cyan);
}
void Game::run()
{
  while (mWindow.isOpen())
  {
    processEvents();
    update();
    render();
  }
}

void Game::processEvents()
{
  sf::Event event;
  while (mWindow.pollEvent(event))
  {
    if (event.type == sf::Event::Closed)
      mWindow.close();
  }
}

void Game::update()
{
 
}

void Game::render()
{
  mWindow.clear();
  mWindow.draw(mPlayer);
  mWindow.display();
}

int main()
{
  Game game;
  game.run();
}
« Last Edit: May 08, 2016, 04:08:27 pm by Erkoren »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Which compilation error you get?

Erkoren

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
As I can see the compiler says that you didn't link to some libs. Post please list of libs to which you linked, maybe you are missing some.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Quote
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This doesn't look like an actual error message, but rather the last line that tells you that there were errors. Look at the full output.

Quote
As I can see the compiler says that you didn't link to some libs.
Really? All I can see is that the linker failed for some unknown reason. A linker error doesn't always mean that you forgot to link something, it can be many other things.
Laurent Gomila - SFML developer

Erkoren

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Hi!

Problem was solved once I put the implementation of the methods into the .cpp file and the class in the .h file.

When it didn't work, the Game.h file had the exact same code from above (minus the main), the Game.cpp was simply empty and the main file had the include and the main function.

I'm sorry to have bothered you something this simple. Anyways thanks for your help.


 

anything