Search code examples
c++classinheritanceauto

failed to determine the correct return type


I have the following simplified files and classes:

Stat.h:

class Stat
{
  auto getMinMaxValue(std::unordered_map< int, int >&);
};

Stat.cpp:

auto Stat::getMinMaxValue(std::unordered_map< int, int >&m)
{
  return std::minmax_element(m.begin(), m.end(), [](const pair<int, int>& p1, const pair<int, int>& p2) { return p1.second < p2.second; });
}

StatCount.h:

class StatCount : public Stat
{   
  void setWeight(std::vector<D> const&, const std::string);
};

StatCount.cpp:

void StatCount::setWeight(vector<D> const& ref, const string type)
{
    auto a = Stat::getMinMaxValue(m_value);
    cout << "MIN: " << a.first->second << endl;
    cout << "MAX: " << a.second->second << endl;
}

Since i declare the function "getMinMaxValue" into the base class Stat if i use the auto return type i got an error:

function 'getMinMaxValue' with deduced return type cannot be used before it is defined

but i failed to remove the auto return type and find the correct syntax to specify the return type of the method "getMinMaxValue"

if i read the documentation on cppreference i see it must be a pair of iterator but how ?


Solution

  • i respond to myself, it seems i have found the solution:

    Stat.h:

    std::pair<std::unordered_map< int, int >::iterator, std::unordered_map< int, int >::iterator> getMinMaxValue(std::unordered_map< int, int >&);