i'm working on an intake assignment for a game development course but i'm stuck with an annoying error by visual studio after splitting my code into classes. The error states LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) So it should be something wrong in my main function. I've looked around and the most logical reason seemt to be wrong subsystem but that's set to console application. Here's my code:
Main.cpp:
#include <Box2D/Box2D.h>
#include <SFML/Graphics.hpp>
#include "Game.hpp"
int main() {
Game game; //Sets up the game
game.populateWorld();
while (game.window.isOpen()) {
game.checkEvents();
game.drawBodies();
game.m_Player->movePlayer();
}
return 0;
}
Game.hpp:
#ifndef Game_hpp
#define Game_hpp
#include <SFML/Graphics.hpp>
#include "World.hpp"
#include "Player.hpp"
class Game {
public:
Game();
void checkEvents();
void drawBodies();//Displays the bodies and keeps the view centered
//Public data members to be used by other classes
sf::Texture backGroundTexture;
sf::Texture playerTex;
sf::Texture platformTex;//Doesn't have a sprite declared since multiple copy's will be made
sf::Sprite backGround;
sf::Sprite player;
sf::View view;
sf::RenderWindow window;
const float m_Scale;//Number of pixels per meter
Player* m_Player = NULL;
World* m_World = NULL;
void populateWorld();
private:
void loadResources();//Loads resources and applies them where needed
};
#endif // !Game_hpp
World.hpp:
#ifndef World_hpp
#define World_hpp
#include <Box2D/Box2D.h>
class World {
public:
//Public member functions
World(const float scale);
~World();
void step();
b2Body* spawnPlayer(float x, float y);
//Public data members
b2World* world = NULL; //Points to the entire world! Handle with care ;)
void createPlatform(b2Vec2 point);
private:
//Private data members
b2Vec2 gravity;
const float m_Scale;
//Declaring step values
float32 m_TimeStep;
int32 m_VelocityIterators;
int32 m_PositionIterators;
};
#endif // !World_hpp
Player.hpp:
#ifndef Player_hpp
#define Player_hpp
#include <Box2D/Box2D.h>
#include "World.hpp"
class Player{
public:
Player(World* world, const float scale, float x, float y);
void movePlayer();
b2Body* m_PlayerBody;//A pointer to the player Character's body needed for the movement
private:
World* m_World;
const float m_Scale;
};
#endif // !Player_hpp
Hope anyone can point me in the right direction with what's going wrong :)
Guess i was ignorant... There was a warning about the main.cpp using line endings for Mac Os X since i work on an iMac i didn't care. But when i fixed this in Notepad++ EDIT -> EOL Conversion -> Windows Format, it worked like a sharm! My bad i'm afraid ^^
I solved my problem by the following:
1) closing the project and restarting the PC
2) Creating a new project and following this thread because I was doing GUI
http://www.bogotobogo.com/cplusplus/application_visual_studio_2013.php
Related
I'm trying to code a simple game, where the main.cpp file instantiates the Game class, and the Game class instantiates the Player class. I want the Player class to have access to the Game class' variables, so I guess the best way to do that is to have the Game class pass itself to the Player class:
game.h
#pragma once
#include <raylib.h>
#include "player.h"
class Game
{
public:
Game();
void run();
private:
void update();
void draw();
Player player;
};
game.cpp
#include "game.h"
Game::Game() : player(this, { 0, 0, 0 })
{
}
void Game::run()
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "teste");
SetTargetFPS(60);
while (!WindowShouldClose())
{
update();
draw();
}
CloseWindow();
}
void Game::update()
{
player.update();
}
void Game::draw()
{
BeginDrawing();
ClearBackground(RAYWHITE);
EndDrawing();
}
player.h
#pragma once
#include <raylib.h>
#include "game.h"
class Player
{
public:
Player(Game* game, Vector3 pos);
void update();
void draw();
Game* game;
Vector3 pos;
};
player.cpp
#include "player.h"
Player::Player(Game* game, Vector3 pos)
{
this->game = game;
this->pos = pos;
speed = .1;
}
void Player::update()
{
if (IsKeyDown(KEY_D)) pos.x += 1;
if (IsKeyDown(KEY_A)) pos.x += -1;
if (IsKeyDown(KEY_S)) pos.z += 1;
if (IsKeyDown(KEY_W)) pos.z += -1;
}
void Player::draw()
{
}
The code seems fine to Intellisense, but the compiler just throws a bunch of errors.
I tried passing this as an argument to the Player class constructor, but it didn't work out, as the compiler doesn't accept it.
You have a circular dependency between your header files.
game.h depends on player.h, because the Game::player member is a Player instance, not a Player* pointer or Player& reference, so the complete Player class declaration is needed. This is fine.
However, player.h depends on game.h, but Game hasn't been declared yet (because of the header guard in game.h) when the Player class declaration is compiled.
Since the Player::game member is just a Game* pointer, you can use a forward declaration to break that header dependency, eg:
game.h
#pragma once
//#include <raylib.h> // <-- game.h doesn't use anything from this, so move it to game.cpp
#include "player.h" // <-- needed here because of Game::player
class Game
{
public:
Game();
void run();
private:
void update();
void draw();
Player player;
};
game.cpp
#include "game.h"
#include <raylib.h> // <-- move here
Game::Game() : player(this, { 0, 0, 0 })
{
}
...
player.h
#pragma once
#include <raylib.h> // <-- leave here if it declares Vector3
//#include "game.h" // <-- move to player.cpp
class Game; // <-- use this forward declaration instead
class Player
{
public:
Player(Game* game, Vector3 pos);
void update();
void draw();
Game* game;
Vector3 pos;
};
player.cpp
#include "player.h"
#include "game.h" // <-- move here
Player::Player(Game* game, Vector3 pos)
{
this->game = game;
...
}
...
In short, a header file should #include only the things it actually uses directly. Anything else should be #include'd in the .cpp file instead. But, anything in a header file that is just a pointer or reference can be forward-declared without using an #include. Keep your #includes to a minimum in header files. The compilation process will be cleaner and more efficient because of it.
I have three classes, 2 of which rely on the functionality of each other, and one which contains pointers to and is included in both:
Player:
#pragma once
#include <SFML\Graphics.hpp>
#include "RenderableObject.h"
//#include "Renderer.h"
class Renderer;
class Player : public RenderableObject
{
public:
Player(int size, Renderer* renderer);
~Player();
void render();
void setX(int x);
void setY(int y);
int getX();
int getY();
void setVelX(float x);
void setVelY(float y);
float getVelx();
float getVely();
int getSize();
private:
int size;
int x;
int y;
float velx;
float vely;
Renderer* ren;
};
GameManager:
#pragma once
#include "Player.h"
class Renderer;
class GameManager
{
public:
GameManager(Renderer* r);
~GameManager();
Player* getBall();
private:
Player* ball;
};
Renderer:
#pragma once
#include <SFML\Graphics.hpp>
#include "GameManager.h"
#include "Player.h"
class Renderer
{
public:
Renderer(GameManager* game);
~Renderer();
sf::RenderWindow* getWindow();
void draw();
void renderCircle(int size, int x, int y);
void renderSquare(int size, int x, int y);
private:
sf::RenderWindow* win;
GameManager* game;
Player* ball;
};
I suspect there is some sort of circular dependency going on here. I managed to reduce the 60 odd errors down to 3, by adding "class Renderer;" to the Player header file. However, I'm now receiving "Use of undefined type" errors.
It might help to give a higher level view of what I want to achieve:
The GameManager object should hold an instance (maybe more) of player. This GameManager object is shared between subsystems such as Renderer so they all have access to the same resources.
The Renderer object should be able to call the Player object's (retrieved from the GameManager object) render() function, which in turn should call back to the Renderer object's renderCircle() object.
I'm hoping this is a simple case of re-arranging the includes, without having to re-design what I've already done.
In a header file, where you only declare variables that are pointers or references, all the compiler needs to know is that a class exists, nothing more. It doesn't need to know how big it it, or what members the class have. Therefore it's enough with a forward declaration.
However, in the source files where the variable is used, objects are created, and member functions are called, then the compiler need the full definition which means you have to include your whole header file.
Taking your code, simplified:
Player.h
#pragma once
class Renderer;
class Player
{
public:
Player(Renderer*);
private:
Renderer* ren;
};
GameManager.h
#pragma once
class Renderer;
class GameManager
{
public:
GameManager(Renderer*);
private:
Renderer* ren;
};
Renderer.h:
#pragma once
class Player;
class GameManager;
class Renderer
{
public:
Renderer(GameManager*);
private:
GameManager* game;
Player* ball;
};
The above header files are about the same that you already have, with the exception that I used forward declarations in Renderer.h instead.
Now the Renderer.cpp source file, where the Player and GameManager objects are actually created and used, we need the full definition from the header files:
#include "Renderer.h"
#include "Player.h"
#include "GameManager.h"
Renderer::Renderer(GameManager* game)
: game(game), ball(new Player)
{
}
// Other functions that calls member function in the `man` and `ball` objects
Of course, you need to #include "Renderer.h" in the Player.cpp and GameManager.cpp source files.
If you have other errors, they are because of other things you done, not because of the circular dependencies, as those have been solved with the forward declarations.
I don't know how to describe this but I declare a class (movableBox) but am not able to use in a specific scope (this scope is in the class player).
Here is some of my code:
player class:
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
#include <vector>
#include "movablebox.h"
class player
{
public:
sf::RectangleShape* player;
sf::Texture* playerTexture;
sf::Vector2f speed;
bool touching;
static void create(sf::Vector2f pos_);
static void draw(sf::RenderWindow& window);
static void updatePos(float& dt, float gravity, std::vector<sf::RectangleShape*> solids, std::vector<movableBox::movableBox_*> movableBoxes);
static void checkControls();
static void checkControlsperEvent (sf::Event& event);
};
#endif // PLAYER_H
and the movableBox class:
#ifndef MOVABLEBOX_H
#define MOVABLEBOX_H
#include <SFML/Graphics.hpp>
#include <vector>
#include "player.h"
class player;
class movableBox
{
public:
struct movableBox_ {
sf::RectangleShape* box;
sf::Texture* texture;
sf::Vector2f speed;
bool selected;
player* selectedBy;
bool touching;
};
static void create(sf::Vector2f pos_, sf::Vector2f size_);
static void draw(sf::RenderWindow& window);
static void updatePos(float& dt, float gravity, std::vector<sf::RectangleShape*> solids);
static void grabNearest(player* player);
};
#endif // MOVABLEBOX_H
I'm getting this error:
CodeBlocksProjects/phys/player.h:18:110: error: ‘movableBox’ was not declared in this scope
As you can tell by my lack of explaination I don't know why or how this happens, I hope you understand my problem.
Thanks in advance! :)
This is a circular dependency issue. There's #include "movablebox.h" in player.h, and also #include "player.h" in movablebox.h.
You have forward delcared class player in movablebox.h and it seems to be enough; so just remove #include "player.h" from movablebox.h.
I'm trying to create an object where the SFML RenderWindow object is passed over as a parameter, but it isn't simply working, it complains all the time about pointer and that I'm using them in the wrong way.
Here you have my .h file:
#include <iostream>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
using namespace sf;
class Shot
{
private:
RenderWindow &mainWindow;
public:
Shot(RenderWindow &window);
void add(float x, float y, float velocity);
};
and here my .cpp
#include "Shot.h"
Shot::Shot(RenderWindow &window) : mainWindow(&window)
{
mainWindow -> window;
}
void Shot::add(float x, float y, float velocity)
{
CircleShape shape(10);
shape.setPosition(Vector2f(x, y));
shape.setFillColor(Color::Yellow);
mainWindow.draw(shape);
}
Errors:
Error 1 error C2248: 'sf::NonCopyable::operator =' : cannot access private member declared in class 'sf::NonCopyable'
Error 2 error C2248: 'sf::NonCopyable::operator =' : cannot access private member declared in class 'sf::NonCopyable'
i honestly have no clue what's the problem by now an I've probably done it all wrong, but any help would really be appreciated! :)
Best Regards
FreeSirenety
In your .cpp file, you do:
Shot::Shot(RenderWindow &window) : mainWindow(&window)
{
mainWindow -> window;
}
but window is a reference, so you simply can do:
Shot::Shot(RenderWindow &window) : mainWindow(window)
{}
Also, I wouldn't use using namespace sf;, it can make the code confusing after awhile.
I have 3 classes namely Board, Game, and AI
i want to have the object chessBoard of Board class to be use by Game and AI, and also by the Board Class. I want them to access that single chessBoard object (sharing)
problem is, it gives me the infamous fatal error LNK1169: one or more multiply defined symbols found. Also, there are Board.h,Game.h and AI.h (only declarations in them), and i also have their corresponding .cpp files. Each .h files have guards included (#ifndef _XXXXXX_H_)
I tried to include Board chessBoard inside Board.h file (just below the class), and it seems guards are not working.
Error 7 error LNK2005: "class Board chessBoard" (?chessBoard##3VBoard##A) already defined in AI.obj C:\Users\xxxx\Documents\Visual Studio 2010\Projects\CHESSv3\CHESSv3\Board.obj CHESSv3
Error 8 error LNK2005: "class Board chessBoard" (?chessBoard##3VBoard##A) already defined in AI.obj C:\Users\xxxxx\Documents\Visual Studio 2010\Projects\CHESSv3\CHESSv3\Game.obj CHESSv3
Error 9 error LNK2005: "class Board chessBoard" (?chessBoard##3VBoard##A) already defined in AI.obj C:\Users\xxxxx\Documents\Visual Studio 2010\Projects\CHESSv3\CHESSv3\ProgramEntryPoint.obj CHESSv3
AI.h
#ifndef _AI_H_
#define _AI_H_
#include <iostream>
#include <string>
using namespace std;
struct node {
string position;
string nextPosition;
float score;
int level;
float totalscore;
node* up;
node* right;
node* left;
bool approved;
string move;
};
class AI {
private:
//string board;
//string board[8][8];
int score1;
int maxscore;
int totalscore;
public:
void GetBoard(string[][8]);
void AnalyzeMyPositions();
void ExecuteAdvanceHeuristicMove();
};
#endif
Game.h
#ifndef _GAME_H_
#define _GAME_H_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
class Game {
public:
char WhosTurn();
bool Playable();
bool GetMoveFromPlayer();
void TurnOver();
Game();
private:
char turn;
};
#endif
Board.h
#ifndef _BOARD_H_
#define _BOARD_H_
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
class Board {
public:
bool SquareChecker(string);
bool MoveChecker(string);
Board();
void PrintBoard();
int Execute(string);
void UnExecute();
string CB[8][8];
private:
char turn;
vector<string> BoardRecord;
stack<string> CBR;
//string CB[8][8];
};
Board chessBoard;
#endif
If you really want to do this, you need to make the declaration of the object extern in the header Board.h:
extern Board chessBoard;
You then provide a declaration in Board.cpp:
Board chessBoard;
Much better, though would be to create it in enclosing code and pass it (by reference) to the constructors of the other classes.
What you are looking for is the Singleton design pattern which can achieved in the following way:
// Board.h
class Board {
private:
static instance_;
public:
static Board *instance();
}
// Board.cpp
Board *Board::instance_ = NULL;
Board *Board::instance() {
if (!instance_)
instance_ = new Board();
return instance_;
}
Mind that this pattern can either be seen as good or bad, if you don't like using it you should consider passing the reference of an instantiated Board class to all the requiring ones and keep it stored in each object as an instance variable. Something like:
Game::Game() {
this->board = new Board();
this->ai = new AI(board);
// or better this->ai = new AI(this) so AI can access all game methods
}
Your problem could be that there could be 3 different chessBoard definitions because you might be adding 3 different #include "Board.h". Please make only a single object at a place where you have more control rather than creating it globally inside Board.h
Did you try it like this? Include the necessary include declarations only in the .cpp files.
//Board.h
class Board {};
//Game.h
class Board;
class Game {
Board* myBoard;
public:
void setBoard(Board*);
};
//AI.h
class Board;
class AI {
Board* myBoard;
public:
void setBoard(Board*);
};
void main() {
Board chessBoard;
Game g;
g.setBoard(&chessBoard);
AI ai;
ai.setBoard(&chessBoard);
}