C++ 在具有正向声明的转换运算符中使用不完整类型无效
我在这样的头文件中定义了一个类(尽管这是一个精简版本): my_class.hppC++ 在具有正向声明的转换运算符中使用不完整类型无效,c++,templates,c++11,C++,Templates,C++11,我在这样的头文件中定义了一个类(尽管这是一个精简版本): 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;
// 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>();
};
#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;//我有我的理由
模板
上我的课{
公众:
模板<
标准::尺寸\u t S=尺寸,
typename=typename std::enable_if::type
>
运算符other_class();
};
模板
上我的课{
公众:
模板<
标准::尺寸\u t S=尺寸,
typename=typename std::enable_if::type
>
运算符other_class();
};
然后是头文件中的另一个类,如下所示:
其他类水电站
// 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>();
};
#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>(...);
}
#包括“my_class.hpp”
模板
其他班级{
公众:
其他类别(…){
// ...
}
我的班级数据[秒];
};
模板
模板
my_类::运算符其他_类(){
返回其他_类(…);
}
在我想要定义转换操作符之前一切都很顺利:/现在我在操作符的实现中遇到了一个关于不完整类型class my_class
的错误
这对我来说似乎很奇怪,因为我一直在使用my_class
作为other_class
的数据,这要求它是完整的(它调用类的默认构造函数)
到底是什么导致了这里的错误
系统是Ubuntu14.04,带有GCC4.8.2您需要在转换运算符的定义中将
std::enable_
作为my class
的第三个参数:
template<typename T, std::size_t Size>
template<std::size_t S, typename>
my_class<T, Size, typename std::enable_if<condition>::type>::
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
operator other_class<T, 1, Size>(){
// return ...
}
编辑2
我认为以下内容适用于iso 14.5.5.3/1:
14.5.5.3类模板专业化成员
my_class.hpp
是否包含other_class.hpp
?否,或者我不会向前声明它,因为当我修改代码以替换诸如条件和省略号之类的伪标识符时,我得到的唯一错误是您使用的other_class
没有模板参数返回other_class(…)
,除非我先包含my_class.h
,否则会出现错误。你能发布一些我可以编译的东西来说明问题吗?@CoffeeandCode是在头文件或.cpp文件中实现的吗?(将模板实现放在.cpp文件中是一个常见错误,会导致你遇到的问题)。我试过这个,编译器抱怨原型与类中的任何原型都不匹配;虽然我的条件很苛刻,并且使编译器错误看起来非常恶心:我不知道这有多大帮助,但我已将代码缩减为。@CoffeeandCode Check edit and linked question。至于条件,您可以使用别名模板来简化如我简化的示例所示。如果不是太多,您可以在此处查看实际文件:和@CoffeeandCode,具体位置?(请检查编辑2,并引用标准)。