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
成员函数。