conversion from std::map<std::basic_string<char>,std::pair<int,int(*)(const std::vector::Mat

12,616

Solution 1

Your method marked as const, and so this has type const functionSet*. Change you first line:

map<string, pair<int,func>>::const_iterator it = functions.begin();

or if your compiler supports C++11 standard:

auto it = functions.cbegin();

Solution 2

Look at the output again:

conversion from
'std::map, std::pair&, cv::Mat&)> >::const_iterator {...}'
to non-scalar type
'std::map, std::pair&, cv::Mat&)> >::iterator {...}'

So it seems that functions is a const map, its begin() returns a const iterator, and it cannot be converted into a normal iterator.

The reason it is constant because because getRandomFunction is a constant member function and therefore it can see the class members as constants.

As you do not modify the map, simply using const_iterator should solve the problem:

map<string, pair<int,func>>::const_iterator it = functions.begin();
Share:
12,616
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have defined new type:

    typedef int(* func) (const std::vector<cv::Mat>&, cv::Mat&);
    

    Then I did class member:

    std::map< std::string, std::pair<int,func> > functions;
    

    In function, in first line:

    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;
    }
    

    I got this error:

    error: conversion from 'std::map, std::pair&, cv::Mat&)> > ::const_iterator {aka std::_Rb_tree_const_iterator, std::pair&, cv::Mat&)> > >}' to non-scalar type 'std::map, std::pair&, cv::Mat&)> >::iterator {aka std::_Rb_tree_iterator, std::pair&, cv::Mat&)> > >}' requested map>::iterator it = functions.begin();