需要帮助解决C ++ boost编译错误

时间:2017-03-29 19:49:46

标签: c++ boost g++

我正在尝试构建一个示例C ++循环缓冲区代码,但仍然遇到declaration of anonymous class must be a definition的问题 可悲的是,我之前能够在我的Mac上运行此代码但现在无法执行此操作,您能否建议找到根本原因。

编译

g++ temp.cpp
In file included from temp.cpp:1:
In file included from /usr/local/include/boost/circular_buffer.hpp:55:
/usr/local/include/boost/circular_buffer/base.hpp:72:1: error: declaration of anonymous class must be a definition
class <int>
^
temp.cpp:9:35: error: implicit instantiation of undefined template 'boost::circular_buffer<int, std::__1::allocator<int> >'
      boost::circular_buffer<int> cb(3);
                                  ^
/usr/local/include/boost/circular_buffer_fwd.hpp:34:7: note: template is declared here
class circular_buffer;
      ^
2 errors generated.

CODE

   #include <boost/circular_buffer.hpp>
   #include <numeric>
   #include <assert.h>

    int main(int /*argc*/, char* /*argv*/[])
   {
      // create a circular buffer of capacity 3
      boost::circular_buffer<int> cb(3);

      // insert some elements into the circular buffer
      cb.push_back(1);
      cb.push_back(2);

      // assertions
      assert(cb[0] == 1);
      assert(cb[1] == 2);
      assert(!cb.full());
      assert(cb.size() == 2);
      assert(cb.capacity() == 3);

      // insert some other elements
      cb.push_back(3);
      cb.push_back(4);

      // evaluate the sum
      int sum = std::accumulate(cb.begin(), cb.end(), 0);

      // assertions
      assert(cb[0] == 2);
      assert(cb[1] == 3);
      assert(cb[2] == 4);
      assert(*cb.begin() == 2);
      assert(cb.front() == 2);
      assert(cb.back() == 4);
      assert(sum == 9);
      assert(cb.full());
      assert(cb.size() == 3);
      assert(cb.capacity() == 3);

      return 0;
   }

G ++版本

g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.4.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Boost版本为1.63.0

===============已解决====================

1 个答案:

答案 0 :(得分:0)

似乎由于某种原因定义了

template <class T, class Alloc> 
class circular_buffer                          // circular_buffer/base.hpp line 72
/*! \cond */
#if BOOST_CB_ENABLE_DEBUG
: public cb_details::debug_iterator_registry
#endif
/*! \endcond */
{ /* ... */ };

已损坏。据推测,您已定义了一些宏,例如

#define circular_buffer <int>