C++ - shared_ptr "Unspecialised class template"错误
C++ - "Unspecialised class template" error with shared_ptr
我有一个类Room,它保存了一个到Option对象的shared_ptrs向量,比如:
private:
vector<shared_ptr<Option> > options;
但由于某些原因,当我构建时,我会出现以下错误:
- "shared_ptr":非专用类模板不能用作模板参数"_Ty"的模板参数,应为实类型
- "std::tr1::shared_ptr":使用类模板需要模板参数列表
奇怪的是,我还有一个shared_ptrs向量,语法完全相同,但这个向量没有问题。
还有很多地方会出现错误"Option':未声明的标识符",这让我觉得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();
};
Option.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
类的代码,因此这个答案中有一些猜测。我假设这个代码是
private:
vector<shared_ptr<Option> > options;
位于Room.h中。您的Option.h文件包括Room.h,因此Room
类在Option
类之前声明。因此,当编译Room
类的析构函数并且shared_ptr
实现试图删除Option
对象时,Option
是一个不完整的类型。
从上面的代码中,我不明白为什么Option.h需要包括Room.h.事实上,它应该是相反的。如果它确实需要包含该文件,您应该能够通过在Room.cpp.中显式声明Room::~Room()
来解决这个问题
编辑:
事实证明~shared_ptr<T>
并不要求T是一个完整的类型。然而,shared_ptr<T>( T* )
和shared_ptr<T>::reset( T* )
确实如此,问题可能是因为vector
上的某些操作正在调用其中一个(更可能是前者)的调用。
vector<shared_ptr<Option >>
你几乎做对了:)
vector<shared_ptr<Option> >
这两个>
字符在触摸时会导致您看到的奇怪错误。它被解释为>>
运算符。
顺便说一句,感谢您按照原样发布代码,而不是重新输入并可能隐藏错误。
相关文章:
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- "Undefined class"作为is_base_of的论据
- 如何将class.cpp和class.hpp编译为一个.o文件
- '{'标记之前的预期类名,然后在预声明时无效使用不完整的类型'class class_name'
- 如何使用"derived input class"创建派生类?
- 错误:"Left of getValue must have class/struct/union"
- ' class a : b ' 和 ' class a : public b ' 之间的继承类不同
- Pybind11 Class Definition
- 在C++中使用没有合作伙伴Class/Cpp文件的头文件是否实用
- C2011 'CMemDC':Visual Studio 2019中的'class'类型重新定义
- TypeError: [c++ addon class] 不是构造函数
- C++ class template
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- Clearing Class Foo with new(pFoo) Foo()
- C++ class vs a library
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 使用"class"关键字,后跟未声明的标识符
- C++ - shared_ptr "Unspecialised class template"错误