operator =对于字符串集是不明确的

时间:2015-12-02 07:18:37

标签: c++

我的基类.h文件

#ifndef ITEM_H
#define ITEM_H

#include <ostream>
#include <set>
#include <string>

using namespace std;
typedef set<string> StringSet;

class Item
{
protected:
    string  title;
    StringSet keywords;
public:
    Item();
    Item(const string& title, const string& keywords);
    virtual ~Item();
    //virtual void addKeywords(string keyword) const;
    string getTitle();
    string getKeywords();

};
#endif

继承的类.cpp文件

#include "Book.h"

Book::Book()
{
    title =     "no title";
    keywords = { "no keywords" }; // error
    author =    "no author";
    pagesNr =   0;

}

为什么我会得到一个&#34; C2593:&#39;运营商=&#39;暧昧&#34;对于这一行keywords = { "no keywords" };,它如何解决? 谢谢。

2 个答案:

答案 0 :(得分:2)

{ "no keywords" }是一个初始化列表,显然Visual Studio不能使用带有inializer列表的std::set<std::string>赋值运算符。 相反,您可以直接使用该集的构造函数:

Book::Book()
 : keywords({ "no keywords" })
{
    title =     "no title";
    author =    "no author";
    pagesNr =   0;

}

答案 1 :(得分:1)

最小的“工作”(给出相同的错误)示例是

#include <string>
#include <set>

int main()
{
    std::set<std::string> keywords;
    keywords = { "no keywords" }; // error

    return 0;
}

你还应该说你使用VS 2013.在VS 2013中不完全支持初始化列表,但是如果用VS 2015编译,一切都编译好了。