2017-10-10 82 views
0

我有以下简化文件和类:未能确定正确的返回类型

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

因为我声明函数“getMinMaxValue”到基类统计,如果我使用自动返回类型我得到了一个错误:

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

,但我无法删除自动返回类型,找到正确的语法指定方法“getMinMaxValue”的返回类型

如果我读cppreference我看到它必须是一对迭代器,但如何的文档?

+0

这可能会帮助你https://stackoverflow.com/questions/18559452/how-to -return最内容的-stdpair –

+2

可能是https://stackoverflow.com/q/40694607/10077 –

+0

@FredLarson的副本:这是不是重复,我不寻找一个解决方案,以保持“自动” ,我想找到方法minmax_element – skualito

回答

0

我对自己作出反应,看来我已经找到了解决办法:

Stat.h:

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