Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.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++_C++11_Templates_Variadic Templates - Fatal编程技术网

C++ 如何定义变量类模板的成员模板函数

C++ 如何定义变量类模板的成员模板函数,c++,c++11,templates,variadic-templates,C++,C++11,Templates,Variadic Templates,我试图用成员模板函数实现一个可变类模板,其模板参数独立于类模板参数,但在定义成员模板时遇到了问题 我将问题简化为尝试编译此文件(抱歉,无法进一步简化): #包括 #包括 #包括 #包括 #包括 #包括 #包括 模板 福班{ 公众: Foo(); 模板 T&at(常数标准::字符串和键); 模板 无效插入(常量标准::字符串和键,常量T和值); 私人: std::元组集; std::无序映射类型到位置; }; 模板 Foo::Foo(){ std::vector type_index{std::t

我试图用成员模板函数实现一个可变类模板,其模板参数独立于类模板参数,但在定义成员模板时遇到了问题

我将问题简化为尝试编译此文件(抱歉,无法进一步简化):

#包括
#包括
#包括
#包括
#包括
#包括
#包括
模板
福班{
公众:
Foo();
模板
T&at(常数标准::字符串和键);
模板
无效插入(常量标准::字符串和键,常量T和值);
私人:
std::元组集;
std::无序映射类型到位置;
};
模板
Foo::Foo(){
std::vector type_index{std::type_index(typeid(Types))…};
对于(size_t i=0;i类型到位置插入({type\u index[i],i});
}
}
模板
T&Foo::at(const std::string和key){
std::type_index type_idx{std::type_index(typeid(T))};
尺寸和位置;
pos=this->type_to_pos.at(type_idx);
return std::get(this->sets_u).at(key);
}
模板
void Foo::insert(常量std::字符串和键、常量T和值){
std::type_index type_idx{std::type_index(typeid(T))};
尺寸和位置;
pos=this->type_to_pos.at(type_idx);
std::get(this->sets);
}
int main(int argc,字符**argv){
富富{};
foo.插入(“键”,1.0f);
std::cout type_to_pos.at(type_idx);
^
/Users/Jasper/cpp_projects/playway/main.cpp:38:24:错误:在项目外部无效使用“this”
非静态成员函数
return std::get(this->sets_u).at(key);
^
/Users/Jasper/cpp_projects/playerd/main.cpp:38:40:错误:使用未声明的标识符“key”
return std::get(this->sets_u).at(key);
^
/Users/Jasper/cpp_projects/playerd/main.cpp:42:21:错误:嵌套名称说明符'Foo:'
for声明不引用类、类模板或类模板部分
专业化
void Foo::insert(常量std::字符串和键、常量T和值){
~~~~~~~~~~~~~~~^
/Users/Jasper/cpp_projects/playway/main.cpp:43:19:错误:重新定义“type_idx”
std::type_index type_idx{std::type_index(typeid(T))};
^
/Users/Jasper/cpp_projects/playde/main.cpp:34:19:注意:前面的定义在这里
std::type_index type_idx{std::type_index(typeid(T))};
^
/Users/Jasper/cpp_projects/playde/main.cpp:44:10:错误:重新定义“pos”
尺寸和位置;
^
/Users/Jasper/cpp_projects/playway/main.cpp:35:10:注意:前面的定义在这里
尺寸和位置;
^
/Users/Jasper/cpp_projects/playway/main.cpp:46:9:错误:在项目外部无效使用“this”
非静态成员函数
pos=this->type_to_pos.at(type_idx);
^
产生了8个错误。
生成[2]:***[CMakeFiles/test.dir/main.cpp.o]错误1
生成[1]:***[CMakeFiles/test.dir/all]错误2
make:**[全部]错误2
我很确定它可以归结为第一个和第五个错误,但我无法找出我做错了什么。为什么
Foo
不引用类模板?我如何修复这个问题

编辑:添加了实用程序库和
insert
的固定返回值

为了简单起见,我删除了所有异常检查

--------
@songyuanyao给出的答案解决了这个问题,但正如@songyuanyao指出的,
get
在编译时不知道
pos
,因此它不会编译。的解决方案有助于解决这个问题。

您应该指定两组模板参数:一组用于封闭类模板,另一组用于模板本身

template<class... Types> // for the enclosing class template
template<class T>        // for the member template
T& Foo<Types...>::at(const std::string& key) {
  ...
}

template<class... Types> // for the enclosing class template
template<class T>        // for the member template
void Foo<Types...>::insert(const std::string& key, const T& value) {
  ...
}
template//用于封闭类模板
模板//用于成员模板
T&Foo::at(const std::string和key){
...
}
模板//用于封闭类模板
模板//用于成员模板
void Foo::insert(常量std::字符串和键、常量T和值){
...
}

这似乎已经解决了这个问题。我还必须包括实用程序库,并更改
insert
不返回任何内容。现在我遇到了
没有匹配的函数来调用'get'std::get(This->sets)。insert({key,value})< /代码>,它似乎与原始问题无关。@ JasPrBrun <代码> POS  >在编译时必须已知变量,否则它不能被指定为模板参数。看来,您必须再次考虑这2个函数的实现。
template<class... Types> // for the enclosing class template
template<class T>        // for the member template
T& Foo<Types...>::at(const std::string& key) {
  ...
}

template<class... Types> // for the enclosing class template
template<class T>        // for the member template
void Foo<Types...>::insert(const std::string& key, const T& value) {
  ...
}