2017-09-22 20 views
2

我試圖用boost::assign::list_of()在類中聲明一個靜態集合。在使用boost :: assign :: list_of時聲明不能用非常量表達式初始化

MyClass.h

class MyClass 
{ 
    public: 
     static std::set<std::string> & formats_set(); 
    private: 
     static const std::set<std::string> formats_; 
} 

MyClass.cpp

const std::set<std::string> MyClass::formats_ = boost::assign::list_of(
    "Format1" 
    ,"Format2" 
    ,"Format3"); 

但是 - 當我嘗試編譯我收到錯誤 ‘MyClass::formats_’ cannot be initialized by a non-constant expression when being declared

有什麼辦法來解決這個問題? 謝謝!

+1

我想你一定會使用C++ 98? – YSC

+0

你是對的:/ – canadiadude

+0

如果下面的答案滿足你,請點擊答案評分正好的綠色選中標記。 – YSC

回答

1

現在讓我們嘗試使用了正確的語法:

#include <string> 
#include <set> 
#include <boost/assign/list_of.hpp> // for 'list_of()' 

class MyClass 
{ 
    public: 
     static std::set<std::string> & formats_set(); 
    private: 
     static const std::set<std::string> formats_; 
}; 

const std::set<std::string> MyClass::formats_ = boost::assign::list_of 
    ("Format1") 
    ("Format2") 
    ("Format3"); 
+1

你打我幾秒鐘:/活:http://coliru.stacked-crooked.com/a/a0d235afcaa211ba(注意:'<>'是不必要的) – YSC

+0

@YSC謝謝。將刪除它們。自從我不得不使用這個優秀的圖書館已經很長時間了。 –

相關問題