我定义了新类型:
typedef int(* func) (const std::vector<cv::Mat>&, cv::Mat&);
Run Code Online (Sandbox Code Playgroud)
然后我做了班级成员:
std::map< std::string, std::pair<int,func> > functions;
Run Code Online (Sandbox Code Playgroud)
在函数中,在第一行:
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;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
错误:从 '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&)> > >}' 请求映射>::迭代器 it = functions.begin();
您的方法标记为 const,因此this具有 type const functionSet*。改变你的第一行:
map<string, pair<int,func>>::const_iterator it = functions.begin();
Run Code Online (Sandbox Code Playgroud)
或者如果您的编译器支持 C++11 标准:
auto it = functions.cbegin();
Run Code Online (Sandbox Code Playgroud)