I have created 2 .cpp files and 2 .header files
- main.cpp (main .cpp file)
- main.hpp (main header file)
- game.cpp
- game.hpp
I have used main.hpp components in game.hpp
Code:
#include <SFML/Graphics.hpp>
#include <bits/stdc++.h>
#include "Window.hpp"
using namespace std;
class Ship{
public:
Ship();
~Ship();
int ship_x=400-28;
int ship_y=600-55;
sf::Texture ship_texture;
sf::Sprite ship_sprite;
Window *window = new Window();
void Ship_Void(){
if(window->event.type==sf::Event::KeyPressed){
if(window->event.key.code==sf::Keyboard::D){
if(ship_sprite.getPosition().x<=544)
ship_sprite.move(sf::Vector2f(0.04, 0));
}
if(window->event.key.code==sf::Keyboard::A){
if(ship_sprite.getPosition().x>=200)
ship_sprite.move(sf::Vector2f(-0.04, 0));
}
if(window->event.key.code==sf::Keyboard::W){
if(ship_sprite.getPosition().y>=0)
ship_sprite.move(sf::Vector2f(0, -0.04));
}
if(window->event.key.code==sf::Keyboard::S){
if(ship_sprite.getPosition().y<=545)
ship_sprite.move(sf::Vector2f(0, 0.04));
}
}
}
void Keys(){
ship_texture.loadFromFile("images/spaceship.png");
ship_sprite.setTexture(ship_texture);
ship_sprite.setPosition(ship_x, ship_y);
}
};
Compilation command:
g++ Window.cpp -o game -lsfml-graphics -lsfml-window -lsfml-system
Compilation error:
In file included from Ship.hpp:3,
from Window.cpp:2:
Window.hpp:5:7: error: redefinition of ‘class Window’
5 | class Window{
| ^~~~~~
In file included from Window.cpp:1:
Window.hpp:5:7: note: previous definition of ‘class Window’
5 | class Window{
Please help with fixing this error!