我尝试使用c ++ 11(我正在使用gcc-4.8.2)
来应用此解决方案// sort algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
using namespace std;
vector<size_t> sort_indexes(const vector<float> &v) {
vector<size_t> idx(v.size());
for (size_t i = 0; i != idx.size(); ++i) idx[i] = i;
sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
return idx;
}
int main () {
std::vector<float> w(4, 0.2f);
w.push_back(0.3f);
std::vector<size_t> idx = sort_indexes(w);
// print out content:
std::cout << "ordering:";
for (std::vector<size_t>::iterator it=idx.begin(); it!=idx.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
但是我收到了一个编译错误:
error: no matching function for call to ‘sort(std::vector<long unsigned int>::iterator, std::vector<long unsigned int>::iterator, sort_indexes(const std::vector<float>&)::__lambda0)’
[&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
error: template argument for ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)’ uses local type ‘sort_indexes(const std::vector<float>&)::__lambda0’
我将不胜感激任何帮助: - )
答案 0 :(得分:1)
我认为如果您没有在编译选项中明确请求C ++ 11支持,则会发生错误。
使用以下命令编译测试程序时,我收到一些错误,包括您报告的错误。我得到的错误略有不同,因为我有g ++ 4.9.1而不是4.8.2:
$ g++ -Wall test.cpp
test.cpp: In function ‘std::vector<long unsigned int> sort_indexes(const std::vector<float>&)’:
test.cpp:14:57: warning: lambda expressions only available with -std=c++11 or -std=gnu++11
[&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
^
test.cpp:14:58: error: no matching function for call to ‘sort(std::vector<long unsigned int>::iterator, std::vector<long unsigned int>::iterator, sort_indexes(const std::vector<float>&)::<lambda(size_t, size_t)>)’
[&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
^
test.cpp:14:58: note: candidates are:
In file included from /usr/include/c++/4.9/algorithm:62:0,
from test.cpp:3:
/usr/include/c++/4.9/bits/stl_algo.h:4676:5: note: template<class _RAIter> void std::sort(_RAIter, _RAIter)
sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
^
/usr/include/c++/4.9/bits/stl_algo.h:4676:5: note: template argument deduction/substitution failed:
test.cpp:14:58: note: candidate expects 2 arguments, 3 provided
[&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
^
In file included from /usr/include/c++/4.9/algorithm:62:0,
from test.cpp:3:
/usr/include/c++/4.9/bits/stl_algo.h:4705:5: note: template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)
sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
^
/usr/include/c++/4.9/bits/stl_algo.h:4705:5: note: template argument deduction/substitution failed:
test.cpp: In substitution of ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<long unsigned int*, std::vector<long unsigned int> >; _Compare = sort_indexes(const std::vector<float>&)::<lambda(size_t, size_t)>]’:
test.cpp:14:58: required from here
test.cpp:14:58: error: template argument for ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)’ uses local type ‘sort_indexes(const std::vector<float>&)::<lambda(size_t, size_t)>’
[&v](size_t i1, size_t i2) {return v[i1] < v[i2];});
^
test.cpp:14:58: error: trying to instantiate ‘template<class _RAIter, class _Compare> void std::sort(_RAIter, _RAIter, _Compare)’
您应该能够通过将-std=c++11
添加到编译器调用命令来消除这些错误:
g++ -Wall -std=c++11 test.cpp