Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法转换';这';指针。。。遗产_C++_Inheritance - Fatal编程技术网

C++ 无法转换';这';指针。。。遗产

C++ 无法转换';这';指针。。。遗产,c++,inheritance,C++,Inheritance,尝试调用函数stringsettoString(stringsetaset)时出错来自继承的类。 基类的头文件: #ifndef ITEM_H #define ITEM_H #include <ostream> #include <set> #include <string> using namespace std; typedef set<string> StringSet; class Item { protected: str

尝试调用函数
stringsettoString(stringsetaset)时出错来自继承的类。
基类的头文件:

#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();

    void addKeywords(string keyword);
    virtual ostream& print(ostream& out) const;
    string getTitle() const;

    string SetToString(StringSet aSet);

};
\ifndef项目
#定义项目
#包括
#包括
#包括
使用名称空间std;
排字机;
类项目
{
受保护的:
字符串标题;
字符串集关键字;
公众:
项目();
项目(常量字符串和标题、常量字符串和关键字);
虚拟~Item();
void addKeywords(字符串关键字);
虚拟ostream&print(ostream&out)const;
字符串getTitle()常量;
字符串集合字符串(字符串集合aSet);
};
基类的实现文件:

#include "Item.h"
...
string Item::SetToString(StringSet aSet) {
    string key;
    int sizeCount = 0;

    for (auto const& e : aSet) {
        key += e;
        sizeCount++;
        if (sizeCount < aSet.size()) {
            key += ", ";
        }
    }
    SetToString(keywords);
    return key;
}
...
#包括“Item.h”
...
字符串项::SetToString(StringSet aSet){
字符串键;
int-sizeCount=0;
用于(自动施工与设备:aSet){
键+=e;
sizeCount++;
if(sizeCount

当我尝试执行
string k=SetToString(关键字)时
在继承的类中,我得到错误:
错误C2662'std::string Item::SetToString(StringSet)':无法将'this'指针从'const Book'转换为'Item&'。
。如何修复此错误,以及我为什么会得到它?

项::SetToString
未标记为
const
,因此无法通过
const
指针或引用,或在
const
对象上调用它

您似乎试图从标记为
const
的函数调用它,因此无法修改当前对象(
this
),包括对其调用非
const
函数


要么使继承的函数不
const
,要么使基函数
const

string k=SetToString(关键字)行中出现该错误
?@immibis是的,在此行中,可能您正在从非
const
成员函数调用
const
成员函数。