无法转换 Return 中的 const 对象
Cannot convert const object in Return
我不完全确定我在这里做错了什么。我有一个 class ,它包含一个指向另一个 class 对象的常量指针。但是,我收到有关无法转换 const(class 对象)的错误。我究竟做错了什么?我的代码设置是否不正确?
错误信息:cannot convert 'const AppProfile' to 'AppProfile*' in return
我最初在我的头文件 class AppProfile
中有这个,我将它更改为 #include "appprofile.h"
这有助于消除另一个错误。
稍后我将调用 run()
,它在我的 AppProfile 对象上执行 run
。
头文件
#ifndef APPITEM_H
#define APPITEM_H
#include <QObject>
#include <QUrl>
#include <QDir>
#include "appprofile.h"
class AppItem : public QObject
{
Q_OBJECT
public:
explicit AppItem(QObject *parent = nullptr);
explicit AppItem(const AppProfile &profile,
QObject *parent);
/// App Profile
AppProfile *profile() const;
signals:
public slots:
void run();
private:
const AppProfile m_profile;
};
#endif // APPITEM_H
cpp文件
#include "appitem.h"
#include "appprofile.h"
AppItem::AppItem(QObject *parent) :
QObject(parent)
{
}
AppItem::AppItem(const AppProfile &profile,
QObject *parent) :
QObject(parent),
m_profile(profile)
{
}
QString AppItem::name() const
{
return m_name;
}
void AppItem::run()
{
AppProfile *profile = profile();
profile->run();
}
AppProfile *AppItem::profile() const
{
return m_profile;
}
更新:
根据给定的答案跟进问题...
为了简单地解释我的意图,我正在解析一个 json 文件,其中包含用于创建父对象 AppItem 的数据。构建此项目时,它会在构建 AppProfile 对象时接受。该对象仅在创建 AppItem 时创建一次。
知道了这一点,您会建议我如何继续编辑与 AppProfile 相关的原始问题代码。假设这是足够的信息。感谢您的帮助。这就是我用来创建 AppItem
的代码
AppProfile *profile = new AppProfile();
AppItem *appItem = new AppItem(profile);
对于初学者,要么是你的代码中有错别字,要么是函数定义不正确
AppProfile *AppItem::profile() const
{
return m_profile;
}
在 class 中,数据成员 m_profile 不是指针。
//...
private:
const AppProfile m_profile;
};
因此,如果数据成员的声明有效,那么函数应该如下所示
const AppProfile *AppItem::profile() const
{
return &m_profile;
}
或者如果数据成员声明应该是这样的
//...
private:
const AppProfile *m_profile;
};
那么在任何情况下函数都应该return一个指向常量数据的指针。
const AppProfile *AppItem::profile() const
{
return m_profile;
}
那是错误消息隐含地说你的代码中有错字
cannot convert 'const AppProfile' to 'AppProfile*' in return
并且如果您在任何情况下都将更新拼写错误,则您不能丢弃指针的限定符 const。
我不完全确定我在这里做错了什么。我有一个 class ,它包含一个指向另一个 class 对象的常量指针。但是,我收到有关无法转换 const(class 对象)的错误。我究竟做错了什么?我的代码设置是否不正确?
错误信息:cannot convert 'const AppProfile' to 'AppProfile*' in return
我最初在我的头文件 class AppProfile
中有这个,我将它更改为 #include "appprofile.h"
这有助于消除另一个错误。
稍后我将调用 run()
,它在我的 AppProfile 对象上执行 run
。
头文件
#ifndef APPITEM_H
#define APPITEM_H
#include <QObject>
#include <QUrl>
#include <QDir>
#include "appprofile.h"
class AppItem : public QObject
{
Q_OBJECT
public:
explicit AppItem(QObject *parent = nullptr);
explicit AppItem(const AppProfile &profile,
QObject *parent);
/// App Profile
AppProfile *profile() const;
signals:
public slots:
void run();
private:
const AppProfile m_profile;
};
#endif // APPITEM_H
cpp文件
#include "appitem.h"
#include "appprofile.h"
AppItem::AppItem(QObject *parent) :
QObject(parent)
{
}
AppItem::AppItem(const AppProfile &profile,
QObject *parent) :
QObject(parent),
m_profile(profile)
{
}
QString AppItem::name() const
{
return m_name;
}
void AppItem::run()
{
AppProfile *profile = profile();
profile->run();
}
AppProfile *AppItem::profile() const
{
return m_profile;
}
更新: 根据给定的答案跟进问题...
为了简单地解释我的意图,我正在解析一个 json 文件,其中包含用于创建父对象 AppItem 的数据。构建此项目时,它会在构建 AppProfile 对象时接受。该对象仅在创建 AppItem 时创建一次。
知道了这一点,您会建议我如何继续编辑与 AppProfile 相关的原始问题代码。假设这是足够的信息。感谢您的帮助。这就是我用来创建 AppItem
的代码AppProfile *profile = new AppProfile();
AppItem *appItem = new AppItem(profile);
对于初学者,要么是你的代码中有错别字,要么是函数定义不正确
AppProfile *AppItem::profile() const
{
return m_profile;
}
在 class 中,数据成员 m_profile 不是指针。
//...
private:
const AppProfile m_profile;
};
因此,如果数据成员的声明有效,那么函数应该如下所示
const AppProfile *AppItem::profile() const
{
return &m_profile;
}
或者如果数据成员声明应该是这样的
//...
private:
const AppProfile *m_profile;
};
那么在任何情况下函数都应该return一个指向常量数据的指针。
const AppProfile *AppItem::profile() const
{
return m_profile;
}
那是错误消息隐含地说你的代码中有错字
cannot convert 'const AppProfile' to 'AppProfile*' in return
并且如果您在任何情况下都将更新拼写错误,则您不能丢弃指针的限定符 const。