Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.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++ 对我的类“使用基于范围的循环时出错”;begin未找到匹配的重载函数;_C++_Loops - Fatal编程技术网

C++ 对我的类“使用基于范围的循环时出错”;begin未找到匹配的重载函数;

C++ 对我的类“使用基于范围的循环时出错”;begin未找到匹配的重载函数;,c++,loops,C++,Loops,我创建了一个名为List的类,该类在内部使用unordered\u map,但在使用基于范围的循环时,会出现一些错误: 错误C2672:“开始”:未找到匹配的重载函数 错误C2672:'end':未找到匹配的重载函数 模板 班级名单{ 私人: //主容器 std::无序映射m_容器; 公众: typename std::无序映射::常量迭代器开始(); typename std::无序映射::常量迭代器End(); //..... }; 模板 typename std::无序映射::常量迭

我创建了一个名为
List
的类,该类在内部使用
unordered\u map
,但在使用基于范围的循环时,会出现一些错误:

  • 错误C2672:“开始”:未找到匹配的重载函数
  • 错误C2672:'end':未找到匹配的重载函数

模板
班级名单{
私人:
//主容器
std::无序映射m_容器;
公众:
typename std::无序映射::常量迭代器开始();
typename std::无序映射::常量迭代器End();
//.....
};
模板
typename std::无序映射::常量迭代器列表::开始(){
返回m_container.cbegin();
}
模板
typename std::无序映射::常量迭代器列表::End(){
返回m_container.cend();
}
使用时:

List<std::wstring, std::wstring> list;
list.Add(L"AKey", L"LionKing");
list.Add(L"BKey", L"Animal");
list.Add(L"CKey", L"Good");

for (auto &item : list) // The error messages appear
    cout << item.second << endl;
列表;
添加(L“AKey”,L“LionKing”);
列表。添加(L“BKey”,L“动物”);
列表。添加(L“CKey”,L“Good”);
for(auto&item:list)//将显示错误消息

cout基于范围的for循环要求任一表达式在
列表上有效
变量:

  • list.begin()
  • 开始(列表)
同上
end

您已选择将相关函数分别命名为
开始
结束

重命名函数,或定义调用这些函数的自由浮动
begin
end
函数,如(未测试):

模板

自动开始(列表)

基于范围的for循环要求任一表达式在
列表
变量上有效:

  • list.begin()
  • 开始(列表)
同上
end

您已选择将相关函数分别命名为
开始
结束

重命名函数,或定义调用这些函数的自由浮动
begin
end
函数,如(未测试):

模板

自动开始(列表)

当约定为
begin
End
时,为什么要调用迭代器
begin
End
?遵守惯例。不要发明你自己的C++。我强烈建议使用完全相同的名称包装
cbegin()
cend()
。@tadman:谢谢,我认为名称不重要。哦,不,名称非常重要。你不能只是编造一些东西,然后期望编译器来解决它。在C++中,必须精确地指定所有的东西。为什么当调用约定时,调用迭代器<代码>开始<代码> >代码>结束/代码>?遵守惯例。不要发明你自己的C++。我强烈建议使用完全相同的名称包装
cbegin()
cend()
。@tadman:谢谢,我认为名称不重要。哦,不,名称非常重要。你不能只是编造一些东西,然后期望编译器来解决它。在C++中,必须精确地指定所有的内容。谢谢,在第二种情况下,<代码>开始()和代码>函数是否与类无关?以及如何在主函数中使用它?我在这里发布的自由函数应该与
List
位于同一名称空间中。谢谢,在第二种情况下,
begin()
函数应该与类无关吗?如何在主函数中使用它?我在这里发布的自由函数应该与
List
List<std::wstring, std::wstring> list;
list.Add(L"AKey", L"LionKing");
list.Add(L"BKey", L"Animal");
list.Add(L"CKey", L"Good");

for (auto &item : list) // The error messages appear
    cout << item.second << endl;
template<class... Args>
auto begin(List<Args...>&& list)
{
   return std::forward<List<Args...>>(list).Beginning();
}