2017-03-29 165 views
-1

我想構建一個示例C++循環緩衝區代碼,但仍然遇到問題declaration of anonymous class must be a definition 可悲的部分是,我能夠在我的Mac上運行此代碼,但不能現在就這樣做,你能建議可以找到根本原因嗎?需要幫助解決C++ boost編譯錯誤

編譯

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 Version是1.63.0

===============已解決====================

+0

我無法在我的MSVC環境中重現此問題。嘗試做一個乾淨的代碼構建。 – Xirema

+0

你使用的是什麼版本的gcc('g ++ --version')? – Elkvis

+0

你使用的是什麼版本的boost? – Walter

回答

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> 
+0

不是沒有宏定義 – user1918858

+0

預處理後查看代碼。你的'circular_buffer/base.hpp'文件看起來像我上面的代碼片段嗎? – Walter

+0

template class /*! \ cond */ #if BOOST_CB_ENABLE_DEBUG :public cb_details :: debug_iterator_registry #endif /*! \ endcond */ { //要求 // BOOST_CLASS_REQUIRE(T,boost,SGIAssignableConcept); // BOOST_CONCEPT_ASSERT((Assignmentable )); // BOOST_CONCEPT_ASSERT((CopyConstructible )); // BOOST_CONCEPT_ASSERT((DefaultConstructible )); //如果circular_buffer將與另一個容器進行比較,則爲required。 // BOOST_CONCEPT_ASSERT((Equ EquComparable )); // BOOST_CONCEPT_ASSERT((LessThanComparable )); – user1918858