[英](class) does not name a type error in c++
請注意,在發布此問題之前,我已經查看了有關堆棧溢出的其他問題。 我還沒有找到任何有效的解決方案。
(這是簡化的)
我有五個文件:
assets.h
#pragma once
#include "SDL2/SDL.h"
#include <map>
#include <string>
// Asset Manager class
class AssetManager {
private:
std::map<std::string, SDL_Texture*> textures;
public:
AssetManager();
~AssetManager();
void addTexture(std::string id, const char *path);
SDL_Texture *getTexture(std::string id);
};
assets.cpp
#include "assets.h"
// Constructor
AssetManager::AssetManager() {}
// Destructor
AssetManager::~AssetManager() {
// Destructor code
}
// Adds texture to "textures"
void AssetManager::addTexture(std::string id, const char *path) {
// Code for adding texture
}
// Gets texture from "textures"
SDL_Texture *AssetManager::getTexture(std::string id) {
// Code for getting texture
}
game.h
#pragma once
class AssetManager; // assets.h include in game.cpp (see below)
class Game {
private:
// Code here
public:
static AssetManager *assets;
// More code here
};
game.cpp
#include "assets.h"
#include "game.h"
AssetManager *Game::assets = new AssetManager();
// Code for game here
other_file.cpp
#include "game.h"
Game::assets->getTexture[<id>] // <-- This is where the error comes from
即使我在game.h
中使用了 class "AssetsManager" 的前向減速,然后將assets.h
包含在game.cpp
中,但當從其他文件中包含時,它會出現此錯誤:
error: invalid use of incomplete type 'class AssetManager'
為什么說“AssetManager”是一個不完整的類型?
原來我在other_file.cpp
中缺少#include
語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.