C++0x lambda 的未定义符号?

发布于 2024-08-29 01:01:39 字数 2162 浏览 3 评论 0原文

我只是在探索 C++0x 中的一些新东西,这时我遇到了一个绊脚石:

#include <list>
#include <cstdio>
using namespace std;

template <typename T,typename F>
void ForEach (list<T> l, F f) {
    for (typename list<T>::iterator it=l.begin();it!=l.end();++it)
        f(*it);
}

int main() {
    int arr[] = {1,2,3,4,5,6};
    list<int> l (arr,arr+6);
    ForEach(l,[](int x){printf("%d\n",x);});
}

无法编译。我收到大量未定义的符号错误。这是 make 的输出:

 i386-apple-darwin9-gcc-4.5.0 -std=c++0x -I/usr/local/include -o func main.cpp
Undefined symbols:
  "___cxa_rethrow", referenced from:
      std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
  "operator new(unsigned long)", referenced from:
      __gnu_cxx::new_allocator<std::_List_node<int> >::allocate(unsigned long, void const*) in ccPxxPwU.o
  "___gxx_personality_v0", referenced from:
      ___gxx_personality_v0$non_lazy_ptr in ccPxxPwU.o
  "___cxa_begin_catch", referenced from:
      std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
  "operator delete(void*)", referenced from:
      __gnu_cxx::new_allocator<std::_List_node<int> >::deallocate(std::_List_node<int>*, unsigned long) in ccPxxPwU.o
  "___cxa_end_catch", referenced from:
      std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
  "std::__throw_bad_alloc()", referenced from:
      __gnu_cxx::new_allocator<std::_List_node<int> >::allocate(unsigned long, void const*) in ccPxxPwU.o
  "std::_List_node_base::_M_hook(std::_List_node_base*)", referenced from:
      void std::list<int, std::allocator<int> >::_M_insert<int const&>(std::_List_iterator<int>, int const&&&) in ccPxxPwU.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [func] Error 1

为什么这不起作用?

I was just poking around into some new stuff in C++0x, when I hit a stumbling block:

#include <list>
#include <cstdio>
using namespace std;

template <typename T,typename F>
void ForEach (list<T> l, F f) {
    for (typename list<T>::iterator it=l.begin();it!=l.end();++it)
        f(*it);
}

int main() {
    int arr[] = {1,2,3,4,5,6};
    list<int> l (arr,arr+6);
    ForEach(l,[](int x){printf("%d\n",x);});
}

does not compile. I get a load of undefined symbol errors. Here's make's output:

 i386-apple-darwin9-gcc-4.5.0 -std=c++0x -I/usr/local/include -o func main.cpp
Undefined symbols:
  "___cxa_rethrow", referenced from:
      std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
  "operator new(unsigned long)", referenced from:
      __gnu_cxx::new_allocator<std::_List_node<int> >::allocate(unsigned long, void const*) in ccPxxPwU.o
  "___gxx_personality_v0", referenced from:
      ___gxx_personality_v0$non_lazy_ptr in ccPxxPwU.o
  "___cxa_begin_catch", referenced from:
      std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
  "operator delete(void*)", referenced from:
      __gnu_cxx::new_allocator<std::_List_node<int> >::deallocate(std::_List_node<int>*, unsigned long) in ccPxxPwU.o
  "___cxa_end_catch", referenced from:
      std::_List_node<int>* std::list<int, std::allocator<int> >::_M_create_node<int const&>(int const&&&) in ccPxxPwU.o
  "std::__throw_bad_alloc()", referenced from:
      __gnu_cxx::new_allocator<std::_List_node<int> >::allocate(unsigned long, void const*) in ccPxxPwU.o
  "std::_List_node_base::_M_hook(std::_List_node_base*)", referenced from:
      void std::list<int, std::allocator<int> >::_M_insert<int const&>(std::_List_iterator<int>, int const&&&) in ccPxxPwU.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
make: *** [func] Error 1

Why is this not working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揽清风入怀 2024-09-05 01:01:39

这些是链接错误。您是否缺少 C++ 库?如果您使用 i386-apple-darwin9-g++-4.5.0 会怎样?

PS 我无法测试这个,因为我的 Mac 上有 gcc 4.2.1 (i686-apple-darwin10-gcc-4.2.1) 并得到:

cc1plus: error: unrecognized command line option "-std=c++0x"

Those are link errors. Are you missing a C++ library? What if you used i386-apple-darwin9-g++-4.5.0?

P.S. I can't test this, as I have gcc 4.2.1 (i686-apple-darwin10-gcc-4.2.1) on my Mac and get:

cc1plus: error: unrecognized command line option "-std=c++0x"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文