簡體   English   中英

命名空間“std”中沒有名為“begin”的成員

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

我在 Windows 上成功編譯了一個應該是跨平台的代碼。 現在,當使用 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

為什么會發生No member named 'begin' in namespace 'std'的錯誤?

std::begin是在 C++11 中引入的。 請參閱此答案以了解如何在 XCode 4.2 中啟用 C++11(方言的確切名稱現在可能已更改)。

或者,如果您無法遷移到 C++11,請切換到std::vector並使用v.begin()

您可以在 C++03 模式下進行編譯。 研究如何讓您的 IDE 在 C++11 模式下編譯。 XCode 4.2 開啟 C++11可能會有所幫助。

std::sort(std::valarray::begin(v), std::valarray::end(v)); ——我不認為標准要求這永遠有效。 我想如果valarray實現了beginend作為靜態或 Koenig 朋友運算符或類似的東西。

std::valarray不帶有成員開始/結束。 在 C++03 中迭代它的唯一方法是使用[]或指針。

valarray保證是連續的。 . 所以你可以寫

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

並在您的valarray上使用notstd::begin

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM