c++ - 如何修复 sfml c++ 代码编译错误?
问题描述
我创建了 2 个 .cpp 文件和 2 个 .header 文件
- main.cpp(主 .cpp 文件)
- main.hpp(主头文件)
- 游戏.cpp
- 游戏.hpp
我在 game.hpp 中使用了 main.hpp 组件
代码:
#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);
}
};
编译命令:
g++ Window.cpp -o game -lsfml-graphics -lsfml-window -lsfml-system
编译错误:
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{
请帮助修复此错误!
解决方案
放在#include "game.hpp"
文件 main.cpp 的顶部
推荐阅读
- angular - 未找到可用的存储方法
- docker - Dockerfile 中的权限被拒绝
- apache-kafka - Zookeeper 是否返回 Kafka 代理 dns?(具有领导者分区)
- excel - VBA IE 查询选择器转换为 Selenium
- node.js - 导出 Swagger Collection 时向 Postman Collection 添加自动测试
- python - 字符串的 Python 子序列
- java - 使用消息进行唯一 = true 验证 - java
- java - 如何在方向更改的片段中保存 Web 视图状态?
- assembly - 使用用户选项更改颜色
- docker - shared:ERROR: BINARYEN_ROOT 在 /root/.emscripten 中设置为空值