【发布时间】:2014-05-08 14:03:32
【问题描述】:
我在这样的头文件中定义了一个类(虽然这是一个精简的版本):
my_class.hpp
// header guard
template<typename T, std::size_t First, std::size_t Second, class Enable = void>
class other_class;
template<typename T, std::size_t Size, class Enable = void>
class my_class; // I have my reasons
template<typename T, std::size_t Size>
class my_class<T, Size, typename std::enable_if<condition1>::type>{
public:
template<
std::size_t S = Size,
typename = typename std::enable_if<other_condition1>::type
>
operator other_class<T, 1, Size>();
};
template<typename T, std::size_t Size>
class my_class<T, Size, typename std::enable_if<condition2>::type>{
public:
template<
std::size_t S = Size,
typename = typename std::enable_if<other_condition2>::type
>
operator other_class<T, 1, Size>();
};
然后头文件中的另一个类是这样的:
other_class.hpp
#include "my_class.hpp"
template<typename T, std::size_t First, std::size_t Second>
class other_class<T, First, Second, typename std::enable_if<condition>::type>{
public:
other_class(...){
// ...
}
my_class<T, First> data[Second];
};
template<typename T, std::size_t Size>
template<std::size_t S, typename>
my_class<T, Size>::operator other_class<T, 1, Size>(){
return other_class<T, 1, Size>(...);
}
在我想定义转换运算符之前一切都很好:/ 现在我在运算符的实现中收到关于不完整类型 class my_class<T, Size> 的错误。
这对我来说似乎很奇怪,因为我一直使用 my_class 作为 other_class 的数据就好了,这需要它是完整的(它调用类的默认构造函数)。
究竟是什么导致了这里的错误?
系统是 Ubuntu 14.04 和 GCC 4.8.2
【问题讨论】:
-
my_class.hpp是否包含other_class.hpp? -
不,否则我不会转发声明
-
我问是因为当我修复你的代码以替换像
condition和省略号这样的虚拟标识符时,我得到的唯一错误是你使用other_class没有模板参数@ 987654330@,除非我首先包含my_class.h,否则会出现错误。 -
你能发布一些我可以实际编译的东西来显示问题吗?
-
@CoffeeandCode 是您在头文件或 .cpp 文件中的实现吗? (将模板实现放在 .cpp 文件中是一个常见错误,会导致您遇到的问题)。