'std::shared_ptr'尚未声明,#include 不起作用<memory>



我知道周围有很多类似的问题,但大多数都能解决包括内存在内的问题。我以前遇到过这个问题,上次这个解决方案似乎解决了这个问题,但这次没有。

我正在和一个使用macbook的伴侣一起做一个大学项目,我使用Windows 10,但通常不是问题。我们正在与Clion和Cmake合作,我们使用SFML库。

目前,这个项目在她的macbook上运行得很好,但在我的macbook上没有运行。代码是相同的,目录是相同的并且位于相同的位置。上次我遇到这个问题,我解决了,正如我所说的,在我们的ResourceHolder.h文件中包括<memory>。现在我无法将该文件包含在我们的Character.h文件中(不知道为什么,就像他们找不到对方一样,但CMakelist.txt包含了我们所有的文件(。当然,我试着浏览我们的文件树,但没有成功。如果我在Character.h中包含memory,那么项目将运行到96%,然后会出现许多未定义的引用错误。

有人能帮我或解释我如何用共享指针一次性解决这个问题吗?我想知道为什么我不能将该文件(ResourceHolder(也包含在同一项目的另一个文件中。


EDIT:编译器是MinGW,下面的代码不包括


编辑<2> :我清理了很多嵌套的#include,然后我像一样更改了Cmake

target_link_libraries(BlackRose core ${SFML_LIBRARIES} )

现在似乎奏效了

这是CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.8)
project(BlackRose)
cmake_policy(SET CMP0074 OLD)
set(SFML_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/Libraries/SFML)
set(CMAKE_CXX_STANDARD 11)
message("Adding test subdir")
add_subdirectory(test)

set(SOURCE_FILES source/Game.cpp include/Game.h 
include/management/ResourceHolder.h source/ProceduralMap.cpp 
include/ProceduralMap.h
include/management/ResourceIdentifier.h source/World.cpp include/World.h 
source/Characters/Character.cpp include/Characters/Character.h
include/Inventory.h source/Characters/PlayerCharacter.cpp 
include/Characters/PlayerCharacter.h source/Objects/Weapon.cpp
include/Objects/Weapon.h source/Objects/Shield.cpp i 
include/Objects/Shield.h source/Characters/Enemy.cpp 
include/Characters/Enemy.h
source/Entity.cpp include/Entity.h source/Objects/Object.cpp 
include/Objects/Object.h source/Objects/ConsumableObject.cpp
include/Objects/ConsumableObject.h source/Objects/RangedWeapon.cpp 
include/Objects/RangedWeapon.h source/Objects/MeleeWeapon.cpp
include/Objects/MeleeWeapon.h source/Projectile.cpp include/Projectile.h 
include/Random.h source/Random.cpp
source/Objects/Tile.cpp include/Objects/Tile.h 
source/Objects/Healpack.cpp include/Objects/Healpack.h 
source/textDisplay.cpp include/textDisplay.h)
add_executable(BlackRose source/main.cpp)

set(CMAKE_MODULE_PATH ${SFML_ROOT}/sfml_cmake)
find_package(SFML REQUIRED system window graphics network audio)
include_directories(${SFML_INCLUDE_DIR})
message("Add library")
add_library(core ${SOURCE_FILES})
target_link_libraries(BlackRose ${SFML_LIBRARIES} core)
get_directory_property(output INCLUDE_DIRECTORIES)
message("Include directories:" ${output})

这是标题:

#ifndef BLACKROSE_CHARACTER_H
#define BLACKROSE_CHARACTER_H

#include <SFML/System.hpp> 
#include "../Inventory.h"
#include "../Objects/Weapon.h"
#include "../Objects/Shield.h"
#include "../Entity.h"
#include <memory>


class Character: public Entity {
public:
Character();
//virtual ~Character() = 0;
virtual void move();
virtual void fight();
//basic interaction with the world, it will be associated with a key
virtual bool interactWithObject(std::shared_ptr <Object> &object);
virtual void die();
void update();
void display();
public:
int hp;
int hpMax;
int attackDamage;
bool barDisplayed = false;
sf::RectangleShape bar;
sf::RectangleShape lifeBar;
protected:
int resistance;
int speed;
sf::Vector2f position;
Inventory inventory;
sf::Clock timerTextures;
};

#endif //BLACKROSE_CHARACTER_H

.cpp文件:

#include "../../include/Characters/Character.h"

Character::Character() {
//all characters are this big
rect.setSize(sf::Vector2f(32,32));
//life bar
bar = sf::RectangleShape(sf::Vector2f(32,5));
lifeBar = sf::RectangleShape(sf::Vector2f(32,5));
bar.setOutlineThickness(3);
bar.setOutlineColor(sf::Color::Black);
bar.setFillColor(sf::Color::Black);
lifeBar.setFillColor(sf::Color::Green);
/*
bar.setPosition(position.x, position.y-16);
lifeBar.setPosition(position.x, position.y-16);
*/

//TODO istanzia un inventory
}
bool Character::interactWithObject(std::shared_ptr <Object> &object) {
return true;
}
...other methods

当我不包括内存时,这些错误就会出现:

In file included from C:UsersLudovicoCLionProjectsBlackRosesourceCharactersCharacter.cpp:5:0:
c:usersludovicoclionprojectsblackroseincludecharacterscharacter.h:26:42: error: 'std::shared_ptr' has not been declared
virtual bool interactWithObject(std::shared_ptr <Object> &object);
^
c:usersludovicoclionprojectsblackroseincludecharacterscharacter.h:26:53: error: expected ',' or '...' before '<' token
virtual bool interactWithObject(std::shared_ptr <Object> &object);
^
C:UsersLudovicoCLionProjectsBlackRosesourceCharactersCharacter.cpp:37:41: error: 'bool Character::interactWithObject' is not a static data member of 'class Character'
bool Character::interactWithObject(std::shared_ptr <Object> &object) {
^
C:UsersLudovicoCLionProjectsBlackRosesourceCharactersCharacter.cpp:37:36: error: 'shared_ptr' is not a member of 'std'
bool Character::interactWithObject(std::shared_ptr <Object> &object) {
^
C:UsersLudovicoCLionProjectsBlackRosesourceCharactersCharacter.cpp:37:59: error: expected primary-expression before '>' token
bool Character::interactWithObject(std::shared_ptr <Object> &object) {
^
C:UsersLudovicoCLionProjectsBlackRosesourceCharactersCharacter.cpp:37:62: error: 'object' was not declared in this scope
bool Character::interactWithObject(std::shared_ptr <Object> &object) {
^
C:UsersLudovicoCLionProjectsBlackRosesourceCharactersCharacter.cpp:37:70: error: expected ',' or ';' before '{' token
bool Character::interactWithObject(std::shared_ptr <Object> &object) {
^
CMakeFilescore.dirbuild.make:104: recipe for target 'CMakeFiles/core.dir/source/Characters/Character.cpp.obj' failed
mingw32-make.exe[2]: *** [CMakeFiles/core.dir/source/Characters/Character.cpp.obj] Error 1
mingw32-make.exe[2]: *** Waiting for unfinished jobs....
In file included from c:usersludovicoclionprojectsblackroseincludecharactersplayercharacter.h:9:0,
from C:UsersLudovicoCLionProjectsBlackRosesourceCharactersPlayerCharacter.cpp:5:
c:usersludovicoclionprojectsblackroseincludecharactersCharacter.h:26:42: error: 'std::shared_ptr' has not been declared
virtual bool interactWithObject(std::shared_ptr <Object> &object);
^
c:usersludovicoclionprojectsblackroseincludecharactersCharacter.h:26:53: error: expected ',' or '...' before '<' token
virtual bool interactWithObject(std::shared_ptr <Object> &object);
^
mingw32-make.exe[2]: *** [CMakeFiles/core.dir/source/Characters/PlayerCharacter.cpp.obj] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/core.dir/all] Error 2
CMakeFilescore.dirbuild.make:118: recipe for target 'CMakeFiles/core.dir/source/Characters/PlayerCharacter.cpp.obj' failed
CMakeFilesMakefile2:108: recipe for target 'CMakeFiles/core.dir/all' failed
mingw32-make.exe: *** [all] Error 2
Makefile:128: recipe for target 'all' failed

谢谢你的耐心和帮助。

尝试用set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")替换set(CMAKE_CXX_STANDARD 11)我以前也遇到过同样的问题,这一行肯定有帮助,也许CMAKE_CXX_STANDARD由于某种原因没有解决任何标志。

最新更新