如何修改程序使其编译?

时间:2012-08-31 04:28:18

标签: c++ stl compiler-errors

#include <iostream>
#include <vector>
#include <algorithm> 

using namespace std;

void push_back(int v, vector<int>& coll) 
{
    coll.push_back(v); 
}

int main() 
{
    int a[] = {1, 2, 3, 4, 5};
    std::vector<int> b;
    for_each(a, a + 5, bind2nd(ptr_fun(push_back), b)); 
}

编译说:

/usr/include/c++/4.1.2/bits/stl_function.h: In instantiation of ‘std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >’:
tt5.cpp:15:   instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:435: error: forming reference to reference type ‘std::vector<int, std::allocator<int> >&’
/usr/include/c++/4.1.2/bits/stl_function.h: In function ‘std::binder2nd<_Operation> std::bind2nd(const _Operation&, const _Tp&) [with _Operation = std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void>, _Tp = std::vector<int, std::allocator<int> >]’:
tt5.cpp:15:   instantiated from here
/usr/include/c++/4.1.2/bits/stl_function.h:455: error: no matching function for call to ‘std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >::binder2nd(const std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void>&, std::vector<int, std::allocator<int> >&)’
/usr/include/c++/4.1.2/bits/stl_function.h:429: note: candidates are: std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >::binder2nd(const std::binder2nd<std::pointer_to_binary_function<int, std::vector<int, std::allocator<int> >&, void> >&)

2 个答案:

答案 0 :(得分:5)

你的程序完全没问题,除了你使用的using namespace std;你应该避免。在这种情况下,可能导致问题。

因此,我建议您删除using namespace std;行,并尝试使用完全限定的名称,例如std::vectorstd::for_each等。

另一种解决办法可能就是这样:只是不要使用std::for_each,因为你不需要它。

写下这个:

int a[] = {1, 2, 3, 4, 5};
std::vector<int> b(a, a+5);

完成!

现在,如果您想稍后插入更多项目,请执行以下操作:

int *c = get_n_items(n); //get n items 
b.insert(b.end(), c, c+n); //insert all items at end

希望有所帮助。


您的真实场景:

正如你在评论中所说:

  

在我的实际工作中,源集合元素是一个对象。我只想从每个元素中获取成员数据并将它们插入到向量中。

如果是这种情况,那么你应该std::transform

假设源是std::vector<person>,并且您希望从此源集合的每个元素中选择 age成员数据,并将它们插入b,这是vector<int> std::vector<person> persons = get_persons(); std::transform(persons.begin(), //input begin iterator persons.end(), //input end iterator std::back_inserter(b), //output iterator select_age); //selector

select_age

其中int select_age(person const & p) { return p.age; } 定义为:

std::transform(persons.begin(),        //input begin iterator
               persons.end(),          //input end iterator
               std::back_inserter(b),  //output iterator
               [](person const & p) {return p.age;});  //selector

如果你可以使用C ++ 11的lambda,那么它就容易多了:

{{1}}

答案 1 :(得分:0)

接近1

void push_back(int v, vector<int>& coll)
{
    coll.push_back(v);
}

for_each(a, a + 5, bind2nd(ptr_fun(push_back), b));

接近2

#include <boost/bind.hpp>
#include <boost/ref.hpp>

for_each(a, a + 5, boost::bind(&vector<int>::push_back, boost::ref(b), _1));

接近3

copy (a, a+5, back_inserter(b));