Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用返回的模板参数创建对象失败,原因是什么?_C++_Templates - Fatal编程技术网

C++ 使用返回的模板参数创建对象失败,原因是什么?

C++ 使用返回的模板参数创建对象失败,原因是什么?,c++,templates,C++,Templates,我想使用一个类中的模板参数来实例化具有相同模板参数的另一个对象。 我试图将参数保存在结构中,但无法将该类型用于新对象 template<typename D>struct Typename { using myType = D; }; template<typename T> class example{ public: Typename<T> someType; }; int main(){ example<dou

我想使用一个类中的模板参数来实例化具有相同模板参数的另一个对象。 我试图将参数保存在结构中,但无法将该类型用于新对象

template<typename D>struct Typename {
    using myType = D;
};

template<typename T>
class example{

public:
    Typename<T> someType;
};

int main(){
        example<double> e1;//ok
        example<e1.someType.myType> e2; //error: cannot refer to member     'myType'
}
templatestruct类型名{
使用myType=D;
};
模板
课例{
公众:
Typename someType;
};
int main(){
示例e1;//确定
示例e2;//错误:无法引用成员“myType”
}
错误消息:

error: invalid use of ‘using myType = double’
     example<e1.someType.myType> e2;
                         ^~~~~~
error: template argument 1 is invalid
     example<e1.someType.myType> e2;
                               ^
error: conflicting declaration ‘example<double> e2’
     example<typename decltype(e1.someType)::myType> e2; //error: cannot refer to member 'myType'
                                                     ^~
错误:“using myType=double”的使用无效
示例e2;
^~~~~~
错误:模板参数1无效
示例e2;
^
错误:声明“示例e2”冲突
示例e2//错误:无法引用成员“myType”
^~

myType
不是
Typename
的数据成员,因此不能使用成员访问语法(
)。您需要使用范围解析运算符(
)从类型本身访问
myType
。例如

example<decltype(e1.someType)::myType>
示例

在此处使用
decltype
。。