![](/img/trans.png)
[英]C++ "error: passing 'const std::map<int, std::basic_string<char> >' as 'this' argument of ..."
[英]conversion from std::map<std::basic_string<char>,std::pair<int,int(*)(const std::vector::Mat
我定義了新類型:
typedef int(* func) (const std::vector<cv::Mat>&, cv::Mat&);
然后我做了班級成員:
std::map< std::string, std::pair<int,func> > functions;
在功能上,在第一行:
pair<funcId,func> functionSet::getRandomFunction() const
{
map<string, pair<int,func>>::iterator it = functions.begin();
std::advance(it, functions.size());
string name = it->first;
func function = it->second.second;
int argumentsNumber = it->second.first;
funcId id = make_pair(argumentsNumber,name);
pair<funcId,func> p = make_pair(id,function);
return p;
}
我收到此錯誤:
錯誤:從'std :: map,std :: pair&,cv :: Mat&)>> :: const_iterator {aka std :: _ Rb_tree_const_iterator,std :: pair&,cv :: Mat&)>>>}'轉換為非-標量類型'std :: map,std :: pair&,cv :: Mat&)>> :: iterator {aka std :: _ Rb_tree_iterator,std :: pair&,cv :: Mat&)> >>}'請求的map> ::迭代器= function.begin();
您的方法標記為const,因此this
類型為const functionSet*
。 更改您的第一行:
map<string, pair<int,func>>::const_iterator it = functions.begin();
或者您的編譯器支持C ++ 11標准:
auto it = functions.cbegin();
再次查看輸出:
conversion from
'std::map, std::pair&, cv::Mat&)> >::const_iterator {...}'
to non-scalar type
'std::map, std::pair&, cv::Mat&)> >::iterator {...}'
因此,似乎functions
是一個const映射,其begin()
返回一個const迭代器,並且無法將其轉換為普通的迭代器。
之所以為常量,是因為getRandomFunction
是常量成員函數,因此可以將類成員視為常量。
由於您不修改地圖,因此只需使用const_iterator
即可解決此問題:
map<string, pair<int,func>>::const_iterator it = functions.begin();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.