C++ 我可以在前向声明的类中使用类型吗?
此类有一个枚举:C++ 我可以在前向声明的类中使用类型吗?,c++,C++,此类有一个枚举: class ThreadController { public: enum ThreadType { ... } } 是否可以使用前向声明类中的ThreadType& class ThreadController; class ThreadWorker { public: static ThreadWorker makeThreadWorker(const ThreadController::ThreadType & type); } 我得到以下错误: '
class ThreadController
{
public:
enum ThreadType { ... }
}
是否可以使用前向声明类中的ThreadType&
class ThreadController;
class ThreadWorker
{
public:
static ThreadWorker makeThreadWorker(const ThreadController::ThreadType & type);
}
我得到以下错误:
'ThreadType' in 'class ThreadController' does not name a type
但是由于我使用的是引用,编译器难道不能满足于头文件中没有定义吗?您可以将
makeThreadWorker
作为模板函数
template <typename T = ThreadController>
static ThreadWorker makeThreadWorker(const typename T::ThreadType & type)
{
}
编译器不知道此时
ThreadController
包含的内容。您如何告诉编译器?typename
?可能重复@MiyazawaKenji,它需要定义。它不可能从一个声明中收集到任何东西。你能把两者颠倒过来吗?改为向前声明ThreadWorker
。
static_assert(std::is_same<ThreadController, T>::value, "error");