包含自己的标头时标准库中的错误

时间:2012-09-11 12:40:12

标签: c++ gcc g++

我有一个bytebuffer类,我自己用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方法,没有任何变化,所以我甚至不必声明一个字节缓冲区,它会抛出错误。什么可能导致这种情况?

1 个答案:

答案 0 :(得分: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列。

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

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

或类似。

解决方案是找到这个标题,并完全删除宏(我最喜欢的),或者将其重命名(进入MIN?),否则在所有标准包含之后移动该标题的包含(并非总是如此)易)。

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