我遇到了一个非常烦人和困扰我的问题。这是代码:
block.hpp:
#pragma once
#include <iostream>
#include <SDL2/SDL.h>
#include "../global/global.hpp"
class Block{
SDL_Texture* texture;
SDL_Rect position;
public:
void setTexture(SDL_Texture* _texture);
void setPosition(SDL_Rect _position);
void render();
void destroy();
};
block.cpp:
#include "block.hpp"
void Block::setTexture(SDL_Texture* _texture){
texture = _texture;
}
void Block::setPosition(SDL_Rect _position){
position = _position;
}
void Block::render(){
SDL_RenderCopy(g_renderer,texture,nullptr,&position);
}
solid.hpp:
#pragma once
#include "block.hpp"
class Solid : public Block{
public:
Solid();
void setPosition(SDL_Rect _position);
void render();
};
solid.cpp:
#include "solid.hpp"
Solid::Solid(){
Block::setTexture(g_assets.textures["cegla"]);
}
void Solid::setPosition(SDL_Rect _position){
Block::setPosition(_position);
}
void Solid::render(){
Block::render();
}
错误:
In file included from src/block/../global/../block/blocks.hpp:3,
from src/block/../global/../map.hpp:8,
from src/block/../global/../assetmanager.hpp:9,
from src/block/../global/global.hpp:6,
from src/block/block.hpp:6,
from src/block/block.cpp:1:
src/block/../global/../block/solid.hpp:5:27: error: expected class-name before ‘{’ token
5 | class Solid : public Block{
请帮助。我不知道是什么原因,我试着用类Block替换#include“block.hpp”,没有任何帮助
1条答案
按热度按时间a0zr77ik1#
编译错误中显示的包含链告诉我们哪里出错了。
block.hpp
包括global.hpp
,global.hpp
包括assetmanager.hpp
,assetmanager.hpp
包括map.hpp
,map.hpp
包括blocks.hpp
,blocks.hpp
包括solid.hpp
,solid.hpp
包括block.hpp
。但是最后一个include已经在include链中,所以它被
#pragma once
忽略了。这意味着当编译器到达Solid
类定义时,它还没有看到Block
类定义,这导致了你得到的错误。你需要重新组织和清理你的标题。