2017-06-29 110 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) –