C++ 无法从‘;QAction*’;至‘;QAction’;

C++ 无法从‘;QAction*’;至‘;QAction’;,c++,qt,plugins,qt4,C++,Qt,Plugins,Qt4,当我尝试返回值时,我在smtdplugin实现文件中遇到了这个错误。好的,我知道我正在创建一个指向QAction的指针,当我试图返回它时,我不能这样做,因为我的方法正在等待对一个对象的引用。但我不知道怎么做(我是个乞丐) 如何避免这个问题,并成功地返回该对象 #include "smtdplugin.h" QAction SmtdPlugin::newItem() { QAction *item = new QAction(NULL); return item; // here i

当我尝试返回值时,我在smtdplugin实现文件中遇到了这个错误。好的,我知道我正在创建一个指向QAction的指针,当我试图返回它时,我不能这样做,因为我的方法正在等待对一个对象的引用。但我不知道怎么做(我是个乞丐) 如何避免这个问题,并成功地返回该对象

#include "smtdplugin.h"

QAction SmtdPlugin::newItem() {

QAction *item = new QAction(NULL);

    return item; // here i get error
}

Q_EXPORT_PLUGIN2(smtdplugin,SmtdPlugin);
头文件:

#ifndef SMTDPLUGIN_H
#define SMTDPLUGIN_H

#include <QObject>
#include <QAction>
#include "smtdinterface.h"

class SmtdPlugin : public QObject,SmtdInterface  {

    Q_OBJECT
    Q_INTERFACES (SmtdInterface)

public :
    QAction newItem();

};

#endif // SMTDPLUGIN_H
\ifndef SMTDPLUGIN\u H
#定义SMTDU插件
#包括
#包括
#包括“smtdinterface.h”
类SmtdPlugin:公共QObject,SmtdInterface{
Q_对象
Q_接口(SMT接口)
公众:
QAction newItem();
};
#endif//SMTDPLUGIN\u H
接口类:

#ifndef SMTDINTERFACE_H
#define SMTDONINTERFACE_H

#include <QAction>

class SmtdInterface {

public:
    virtual ~SmtdInterface() {}
    SmtdInterface();
    virtual QAction newItem () = 0;

};

Q_DECLARE_INTERFACE(SmtdInterface,"com.trololo.Plugin.SmtdInterface/1.0")

#endif 
\ifndef smt接口
#定义SMTDONINTERFACE_H
#包括
类SMT接口{
公众:
虚拟~SmtdInterface(){}
SMT接口();
虚拟QAction newItem()=0;
};
Q_DECLARE_接口(SmtdInterface,“com.trololo.Plugin.SmtdInterface/1.0”)
#恩迪夫

我不熟悉qt,但错误是-

QAction* SmtdPlugin::newItem()   // Return type should be QAction* and not QAction
{

    QAction *item = new QAction(NULL);

    return item;
}
item
的类型是QAction*而不是QAction,这正是编译器所抱怨的。我认为你对空间感到困惑

QAction* item ; // QAction * item ; QAction *item ;

上述三种约定的含义相同。

当您执行
返回项时,您返回的是指向
QAction
的指针,但根据函数的声明,您返回的是
QAction
,因此会出现错误

因此,你应该:

QAction* SmtdPlugin::newItem() {

    QAction *item = new QAction(NULL);

    return item;
}

如果需要在堆上创建操作,那么最好只将返回值更改为代码中的QAction*

,返回类型为
QAction*
的项:

return item; // here i get error
但函数签名要求您返回QAction:

QAction SmtdPlugin::newItem() {
要解决冲突,请将函数签名更改为

QAction* SmtdPlugin::newItem() {

更改函数的概念,使其不再返回指针。

在本例中,您返回的是指向对象的指针,但成员函数的返回类型表示它返回对象。指向一个对象的指针和它自身的对象是两种不同的类型,它们并不相同。因为答案解决了问题,所以我不会投反对票。第一个建议将被否决。QAction是QObject,因此无法复制。您的return语句将尝试复制,但无法编译请删除您的第一个建议,因为它无效