C++ - shared_ptr 出现 "Unspecialised class template"错误

标签 c++ templates shared-ptr

我有一个类 Room,它包含一个指向 Option 对象的 shared_ptr vector ,如下所示:

private:
vector<shared_ptr<Option> > options;

但由于某种原因,当我构建时,出现以下错误:

  • “shared_ptr”:非专用类模板不能用作模板参数“_Ty”的模板实参,需要一个真实类型
  • 'std::tr1::shared_ptr' : 使用类模板需要模板参数列表

奇怪的是,我还有一个 shared_ptr vector ,语法完全相同,但没有问题。

还有一堆地方报错“'Option': undeclared identifier”,这让我觉得可能是Option类的问题,不过好像没问题。这是选项的代码:

选项.h:

#pragma once
#include "Room.h"
#include <memory>

using namespace std;

class Option
{
protected:
    int id;
    char* text;

public:
    Option(void);
    Option(int, char*);
    virtual ~Option(void);
    char* getText();
    int getID();
};

选项.cpp:

#include "Option.h"
#include "Room.h"
#include <memory>
using namespace std;

Option::Option(void)
{
}

Option::Option(int newID, char* newText){
    id = newID;
    text = newText;
}

Option::~Option(void)
{
}

char* Option::getText(){
    return text;
}

int Option::getID(){
    return id;
}

最佳答案

由于您尚未发布 Room 的代码,因此此答案中存在一些推测。类(class)。我假设这段代码

private:
vector<shared_ptr<Option> > options;

Room.h 中。您的 Option.h 文件包含 Room.h,因此 Room类在 Option 之前声明类(class)。所以OptionRoom 时是不完整的类型类的析构函数被编译并且 shared_ptr实现尝试删除 Option对象。

从上面的代码来看,我不明白为什么Option.h需要包含Room.h,其实应该是反过来的。如果它确实需要包含该文件,您应该能够通过显式声明 Room::~Room() 来解决该问题。在 Room.cpp 中越线。

编辑:
结果 ~shared_ptr<T> does not require T to be a complete type .然而,shared_ptr<T>( T* )shared_ptr<T>::reset( T* )做,问题可能是因为对 vector 进行了一些操作正在调用其中一个(更有可能是前者)。

关于C++ - shared_ptr 出现 "Unspecialised class template"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11461609/

相关文章:

c++ - 简单的 C++ vector/指针失误

c++ - Clang (OS X) 在特定的嵌套声明中需要 "template"关键字,而 VS 禁止它

c++ - 错误 2664 - 无法从 std::vector<...> 转换为 std::tr1::shared_ptr

c++ - Boost async_read_some 不完全是异步的

c++ - 延迟构造的 shared_ptr

c++ - 与模板一起使用的最佳 C++ 代码覆盖率工具是什么?

c++ - 元素数量未知的内存分配

c++ - 初始化虚拟继承层次结构

python - 将变量传递到神社导入或从父 html 文件包含

css - Wordpress 跳出模板