1
我想用具有可選值的映射初始化shared_ptr。我將在程序的後期階段初始化這些值。如何使用可選值初始化shared_ptr映射
我讀了下面的帖子,並用它作爲指導:How to add valid key without specifying value to a std::map?
但是我的情況有點不同,因爲我使用一個shared_ptr。事不宜遲,這是我寫的代碼:
ShaderProgram.h
...
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/optional.hpp>
typedef map<string, optional<GLuint> > attributes_map;
class ShaderProgram
{
public:
ShaderProgram(vector<string> attributeList);
...
private:
shared_ptr<attributes_map> attributes;
};
ShaderProgram.mm
ShaderProgram::ShaderProgram(vector<string> attributeList)
{
// Prepare a map for the attributes
for (vector<string>::size_type i = 0; i < attributeList.size(); i++)
{
string attribute = attributeList[i];
attributes[attribute];
}
}
編譯器會通知我,以下錯誤:類型 'shared_ptr的' 不提供一個下標操作符。
任何想法可能是什麼問題?
@LucDanton,同意和更新。 – hmjd 2012-07-30 10:36:38
感謝運行時錯誤的擡頭;) – polyclick 2012-07-30 11:01:31