2017-06-29 47 views
1

我想在windows中編譯nghttp2,opensource。我已經在Linux中成功編譯了。在Visual Studio中std :: begin和std :: end迭代器

我面臨從下面的一段代碼編譯錯誤。 *

template <size_t N> struct Memchunk { 
public: 
    Memchunk(Memchunk *next_chunk) 
     : pos(std::begin(buf)), last(pos), knext(next_chunk), next(nullptr) {} 
    size_t len() const { return last - pos; } 
    size_t left() const { return std::end(buf) - (const size_t) last; } 
    void reset() { pos = last = std::begin(buf); } 
    std::array<uint8_t, N> buf; 
    uint8_t *pos, *last; 
    Memchunk *knext; 
    Memchunk *next; 
    static const size_t size = N; 
}; 

*

我收到以下錯誤。

error C2440: 'return': cannot convert from'std::_Array_const_iterator<_Ty,16384>' to 'std::size_t' 

我在Linux中沒有遇到上述錯誤。我是否缺少特定於Visual Studio的內容?

+3

看起來很不好C++代碼。 – Stargateur

+0

gcc也不喜歡它。 http://ideone.com/SJNXbJ。什麼編譯器接受這個?這個錯誤對我來說很有意義。 – Persixty

+3

爲什麼不'std :: array :: iterator pos,last;'?我不明白。 – ZDF

回答

0

該問題可能源自於在Linux上返回指針 的std::begin()以及Windows中的適當迭代器。這加上一些錯誤。

如果添加beginend便民功能的類,像這樣:

template <size_t N> struct Memchunk { 
    uint8_t* begin() { return buf.data(); } 
    uint8_t* end() { return buf.data()+N; } 
    const uint8_t* begin() const { return buf.data(); } 
    const uint8_t* end() const { return buf.data()+N; } 
    // rest not shown 
}; 

然後left()能等來實現:

size_t left() const { return end() - last; } 

可能與投沉默警告

pos會員現在可以用begin()初始化爲m燼初始化列表

+0

我寫了一個小程序,與實際問題有關,它在Linux中編譯,但不在Visual Studio中編譯。 '#include using namespace std; int foo(std :: array &a) { unsigned int * b = a.begin(); return a.end() - b; } int main(void){ std :: array a = {1,2,3,4}; (a); ''我在Visual Studio中得到以下錯誤' 錯誤C2678二進制' - ':沒有發現操作符需要'std :: _ Array_iterator <_Ty,10>'(或沒有可接受的轉換)的左手操作數 ' Ideone鏈接[鏈接](http://ideone.com/a23Lzq) –

相關問題