0

我有一个字节缓冲区类,由我自己用 c++ 编写,我可以编译它并且它可以正常工作。现在我想在一个大项目中使用它,但是当我将它包含在另一个标题中时,如果我尝试构建它会引发错误。这是错误:

  CXX    out.o
In file included from /usr/include/c++/4.6/streambuf:808:0,
                 from /usr/include/c++/4.6/ios:44,
                 from /usr/include/c++/4.6/ostream:40,
                 from /usr/include/c++/4.6/iostream:40,
                 from /path/to/file/src/someheader1.h:29,
                 from /path/to/file/src/someheader2.h:31,
                 from /path/to/file/src/somesource.cpp:24:
/usr/include/c++/4.6/bits/streambuf.tcc: In member function ‘virtual std::streamsize std::basic_streambuf<_CharT, _Traits>::xsgetn(std::basic_streambuf<_CharT, _Traits>::char_type*, std::streamsize)’:
/usr/include/c++/4.6/bits/streambuf.tcc:56:38: error: expected unqualified-id before ‘(’ token
/usr/include/c++/4.6/bits/streambuf.tcc: In member function ‘virtual std::streamsize std::basic_streambuf<_CharT, _Traits>::xsputn(const char_type*, std::streamsize)’:
/usr/include/c++/4.6/bits/streambuf.tcc:90:38: error: expected unqualified-id before ‘(’ token
In file included from /usr/include/c++/4.6/istream:859:0,
                 from /usr/include/c++/4.6/iostream:41,
                 from /path/to/file/src/someheader1.h:29,
                 from /path/to/file/src/someheader2.h:31,
                 from /path/to/file/src/somesource.cpp:24:
/usr/include/c++/4.6/bits/istream.tcc: In member function ‘std::streamsize std::basic_istream<_CharT, _Traits>::readsome(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize)’:
/usr/include/c++/4.6/bits/istream.tcc:693:46: error: expected unqualified-id before ‘(’ token
make[4]: *** [out.o] Error 1

项目中头文件的骨架:

#ifndef _GUARD_
#define _GUARD_

#include <string>
#include <vector>

...

#include "bytebuffer.h"

namespace A {
  namespace B {

    using namespace std;
    using namespace A::C;

    class Myclass {
    public: 

    ...

     virtual void save( ByteBuffer& bb ) {
     }

     ...    

    } ;
  }
}

#endif  

字节缓冲区非常简单,它包含一个数据向量,以及不同类型的 put 和 get 方法。如果我在标头中注释掉 save 方法,没有任何变化,所以我什至不必声明字节缓冲区,它会引发错误。什么可能导致这种情况?

4

1 回答 1

7

看看这个错误:

/usr/include/c++/4.6/bits/streambuf.tcc:56:38: error: expected unqualified-id before ‘(’ token

我碰巧有这个(希望)非常相同的文件,第 56 行是:

const streamsize __len = std::min(__buf_len, __remaining);
                              ^
                              col 38

为方便读者,我还标记了第 38 列。

哦!该错误涉及(令牌,但在此位置不是 a(而是 a min。所以我的猜测是您项目中的某些标题正在定义一个宏min

#define min(a, b) ((a) < (b) ? (a) : (b))

或类似的。

解决方案是找到此标头,并完全删除宏(我最喜欢),或将其重命名(变为MIN?),或者在所有标准包含后移动该标头的包含(并不总是很容易)。

作为脚注,无处不在的 MS<windows.h>以拥有这样的宏而闻名……也许有人复制了一点?

于 2012-09-11T12:50:30.467 回答