简体   繁体   English

命名空间“std”中没有名为“begin”的成员

[英]No member named 'begin' in namespace 'std'

I successfully compiled on Windows a code that should be crossplatform.我在 Windows 上成功编译了一个应该是跨平台的代码。 Now when compiling it in Xcode with Mac OS X, I get:现在,当使用 Mac OS X 在 Xcode 中编译它时,我得到:

std::valarray<float> v(32);
...
std::sort(begin(v), end(v));            # Use of undeclared identifier 'begin'
std::sort(std::begin(v), std::end(v));  # No member named 'begin' in namespace 'std'
std::sort(std::valarray::begin(v), std::valarray::end(v));  # Idem, error as well

Why does the error No member named 'begin' in namespace 'std' happen?为什么会发生No member named 'begin' in namespace 'std'的错误?

std::begin was introduced with C++11. std::begin是在 C++11 中引入的。 See this answer for how to enable C++11 in XCode 4.2 (the precise name of the dialect has probably changed by now).请参阅此答案以了解如何在 XCode 4.2 中启用 C++11(方言的确切名称现在可能已更改)。

Alternatively, if you can't move to C++11, switch to std::vector and use v.begin() .或者,如果您无法迁移到 C++11,请切换到std::vector并使用v.begin()

You could be compiling in C++03 mode.您可以在 C++03 模式下进行编译。 Work out how to get your IDE to compile in C++11 mode.研究如何让您的 IDE 在 C++11 模式下编译。 XCode 4.2 turn on C++11 may help. XCode 4.2 开启 C++11可能会有所帮助。

std::sort(std::valarray::begin(v), std::valarray::end(v)); -- I don't think the standard demands this ever work. ——我不认为标准要求这永远有效。 I guess if valarray implemented begin and end as statics or Koenig friend operators or somesuch.我想如果valarray实现了beginend作为静态或 Koenig 朋友运算符或类似的东西。

std::valarray doesn't come with member begin/end. std::valarray不带有成员开始/结束。 The only way in C++03 to iterate over it was to use [] or with pointers.在 C++03 中迭代它的唯一方法是使用[]或指针。

valarray is guaranteed to be contiguous. valarray保证是连续的。 . . So you can write所以你可以写

namespace notstd {
  // addressof taken from http://en.cppreference.com/w/cpp/memory/addressof
  template<class T>
  T* addressof(T& arg) {
    return reinterpret_cast<T*>(
           &const_cast<char&>(
              reinterpret_cast<const volatile char&>(arg)));
  }

  template<class T>
  T* begin( std::valarray<T>& v ) { return addressof(v[0]); }
  template<class T>
  T* end( std::valarray<T>& v ) { return begin(v)+v.size(); }
  template<class T>
  T const* begin( std::valarray<T> const& v ) { return addressof(v[0]); }
  template<class T>
  T const* end( std::valarray<T> const& v ) { return begin(v)+v.size(); }
}

and use notstd::begin on your valarray .并在您的valarray上使用notstd::begin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM