我在C ++中有这段代码
class ConstantTest
{
public:
typedef set<string> StrSet;
ConstantTest (const uint16_t & id = 0, const string & str = "Empty",
StrSet & set = StrSet()) :
// Works with: const StrSet & set = StrSet()
m_id (id), m_str (str), m_set (set)
{ }
private:
uint16_t m_id;
string m_str;
StrSet m_set;
};
我想知道为什么在const
参数中没有StrSet
关键字的情况下它无效。关键字工作得很好,但没有它我收到这条消息:
test_const.cpp:17:41: error: could not convert ‘std::set<std::basic_string<char> >()’ from ‘ConstantTest::StrSet {aka std::set<std::basic_string<char> >}’ to ‘ConstantTest::StrSet& {aka std::set<std::basic_string<char> >&}’
StrSet & set = StrSet()) :
^