1

I'm not entirely sure what I'm doing incorrectly here. I have a class which contains a constant pointer to another class object. However I'm getting an error about not being able to convert the const (class object). What am I doing wrong? Is my code setup incorrect in the what I'm trying to do?

Error message: cannot convert 'const AppProfile' to 'AppProfile*' in return

I initially had this in my header file class AppProfile and i changed it to #include "appprofile.h" which helped remove another error.

I later will call run() which executes run on my AppProfile object.

header file

#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 file

#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;
}

UPDATE: Follow up question reguarding the answer givens given...

To simply explain my intentions, I'm parsing a json file that contains data used to create the parent object AppItem. When this item is constructed, it takes in it's construct an AppProfile object. This object is only ever created once, at the time in which AppItem is created.

Knowing that, how would you suggest i move forward editing the original questions code relating to AppProfile. Assuming that's enough information. I appreciate you help. This is what the code looks like that I would use to create an AppItem

AppProfile *profile = new AppProfile();
AppItem *appItem = new AppItem(profile);
JokerMartini
  • 5,674
  • 9
  • 83
  • 193
  • 4
    `AppProfile *profile() const;` -> `const AppProfile* profile() const;` – SergeyA Sep 27 '19 at 14:53
  • 3
    "_cannot convert 'const AppProfile' to 'AppProfile' in return*_" Please copy-paste the error message, instead of paraphrasing it. You are returning `const AppProfile` from function that is declared to return `AppProfile *`, hence you can't get the error that you are claiming to. `AppProfile` is not the same as `AppProfile *`. – Algirdas Preidžius Sep 27 '19 at 14:54
  • 1
    Remember, in `const` functions all members of the class are treated as if they were declared as `const`. – NathanOliver Sep 27 '19 at 14:54
  • 1
    You might find the error message easier to understand if you reduced your code to a [mcve]. Take everything out of `AppItem` except the function member that has the error and the data member required for that function. – JaMiT Sep 27 '19 at 15:09
  • 1
    @AlgirdasPreidžius The error message was copied. However, the formatting of the error message hid an asterisk. (The message was enclosed in asterisks to italicize it. However, the asterisk within the message ended the formatting early. The asterisk in the message became invisible, and the asterisk that was intended to end the formatting appeared as a stray mark at the end of the error message.) – JaMiT Sep 27 '19 at 15:10

1 Answers1

4

For starters either there is a typo in your code or the function is defined incorrectly

AppProfile *AppItem::profile() const
{
    return m_profile;
}

Within the class the data member m_profile is not a pointer.

//...
private:

    const AppProfile m_profile;
};

So if the declaration of the data member is valid then the function should look like

const AppProfile *AppItem::profile() const
{
    return &m_profile;
}

Or if the data member declaration should look like

//...
private:

    const AppProfile *m_profile;
};

then in any case the function shall return a pointer to constant data.

const AppProfile *AppItem::profile() const
{
    return m_profile;
}

That is the error message implicitly says that there is a typo in your code

cannot convert 'const AppProfile' to 'AppProfile*' in return

And if you will update the typo in any case you may not discard the qualifier const for the pointer.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Upvoted and updated the answer to fit the edited question. I'm not sure if that changed the context for your two last sentences though. – Ted Lyngmo Sep 27 '19 at 15:19
  • Is there a reason to use one over the other in either case? – JokerMartini Sep 27 '19 at 17:19
  • @JokerMartini It depends on your intention whether you may and are goung to change the pointed object or you may not. – Vlad from Moscow Sep 27 '19 at 17:20
  • To simply explain, I'm parsing an json file that contains data used to create the parent object AppItem. When this item is constructed, it takes in it's construct an AppProfile. This object is only ever created once, at the time in which AppItem is created. Knowing that what you would you suggest i do? ASsuming that's enough information. I appreciate you help – JokerMartini Sep 27 '19 at 17:27
  • @JokerMartini Try to get that information into you original question by [editing](https://stackoverflow.com/posts/58136919/edit) it. – Ted Lyngmo Sep 27 '19 at 18:07
  • @TedLyngmo I did as you said and updated my original question. Hopefully it makes sense and you can share your wisdom. Thanks – JokerMartini Sep 27 '19 at 19:34
  • @JokerMartini Much better. You often don't need to mark your changes or additions "Edit:" etc. Just update the original question so it gets better. Btw, the answer Vlad has given seems pretty good. Is there anything that is still unclear? – Ted Lyngmo Sep 27 '19 at 22:14
  • @TedLyngmo thanks for your remarks. I see Vlad appears to give a few solutions based on my code. However what I'm curious to know is which of his examples do you suggest i go with? Which one is more ideal for my situation? – JokerMartini Sep 28 '19 at 14:35