Advertisement

Why am I getting all these edefinition errors when I try to introduce OOP to my game engine?

Started by May 26, 2019 02:13 PM
2 comments, last by fleabay 5 years, 3 months ago

Hi I tried to introduce some inheritance to my game engine from the book SDL Game Development but I am getting a lot of errors. I am a real noob at c++ so I would appreciate a bit of detail about each error and what I can do to fix it, as far as I can tell the code is exactly the same as in the book on chapter 3 “Working with game objects” Please tell me what I am doing wrong to produce these errors and how I can fix them. Thanks.

g++ main.cpp -fpermissive -lSDL2 -lSDL2_image
In file included from main.cpp:7:0:
GameObject.h: In member function ‘void Player::draw()’:
GameObject.h:30:18: error: no matching function for call to ‘Player::draw()’
GameObject::draw()
^
GameObject.h:8:6: note: candidate: void GameObject::draw(SDL_Renderer*)
void draw(SDL_Renderer* pRenderer);
^~~~
GameObject.h:8:6: note: candidate expects 1 argument, 0 provided
In file included from main.cpp:8:0:
Player.h: At global scope:
Player.h:4:7: error: redefinition of ‘class Player’
class Player : public GameObject
^~~~~~
In file included from main.cpp:7:0:
GameObject.h:25:7: note: previous definition of ‘class Player’
class Player : public GameObject
^~~~~~
In file included from main.cpp:11:0:
Game.cpp: In member function ‘void Game::render()’:
Game.cpp:84:11: error: no matching function for call to ‘GameObject::draw()’
m_go.draw();
^
In file included from main.cpp:7:0:
GameObject.h:8:6: note: candidate: void GameObject::draw(SDL_Renderer*)
void draw(SDL_Renderer* pRenderer);
^~~~
GameObject.h:8:6: note: candidate expects 1 argument, 0 provided
In file included from main.cpp:11:0:
Game.cpp:86:26: error: no matching function for call to ‘Player::draw(SDL_Renderer*&)’
m_player.draw(m_pRenderer);
^
In file included from main.cpp:7:0:
GameObject.h:28:6: note: candidate: void Player::draw()
void draw()
^~~~
GameObject.h:28:6: note: candidate expects 0 arguments, 1 provided
In file included from main.cpp:13:0:
Player.cpp: At global scope:
Player.cpp:1:77: error: no ‘void Player::load(int, int, int, int, std::__cxx11::string)’ member function declared in class ‘Player’
void Player::load(int x, int y, int width, int height, std::string textureID)
^
Player.cpp:6:6: error: prototype for ‘void Player::draw(SDL_Renderer*)’ does not match any in class ‘Player’
void Player::draw(SDL_Renderer* pRenderer)
^~~~~~
In file included from main.cpp:7:0:
GameObject.h:28:6: error: candidate is: void Player::draw()
void draw()
^~~~

Game.cpp

Game.h

GameObject.cpp

GameObject.h

main.cpp

Player.cpp

Player.h

TextureManager.cpp

TextureManager.h

From a short check, in order to inherit methods, they must be declared virtual in the base class.

Also, class Player is declared in GameObject.h as well as in Player.h. Declarations may only occur once.

Outline:



#pragma once

class GameObject {
private:
	.......

protected:
	virtual void draw();

}


class Player : protected GameObject {
private:
	........

public:
	virtual void draw() override;

}

Edit: and i suggest to exchange the compiler switch -fpermissive with -Wall. Allways good to know where there might be potential problems in the code.

 

Advertisement
3 hours ago, Alio said:

from the book SDL Game Development

This is the absolute worst book I have ever read about game dev. Had I not found the thread below, I would have quit programming altogether. (I was just learning)

https://cboard.cprogramming.com/cplusplus-programming/160170-sdl-image-want-load-because-following-code.html

Start reading from post 10 and pay attention to all posts by Alpo. Thanks Alpo if you ever read this!!!

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

This topic is closed to new replies.

Advertisement