首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >`QObject`子类和复制构造函数出错:`QObject::QObject(const QObject&)为private`

`QObject`子类和复制构造函数出错:`QObject::QObject(const QObject&)为private`
EN

Stack Overflow用户
提问于 2009-11-23 02:02:13
回答 3查看 14.2K关注 0票数 8

下面的编译错误就是我所知道的:

代码语言:javascript
复制
/usr/lib/qt-3.3/include/qobject.h: In copy constructor Product::Product(const Product&):
/usr/lib/qt-3.3/include/qobject.h:211: error: QObject::QObject(const QObject&) is private
Product.h:20: error: within this context
HandleTCPClient.cpp: In member function int Handler::HandleTCPClient(int, std::string, std::string):
HandleTCPClient.cpp:574: note: synthesized method Product::Product(const Product&) first required here 
HandleTCPClient.cpp:574: error:   initializing argument 1 of std::string productDetails(Product)
/usr/lib/qt-3.3/include/qobject.h: In member function Product& Product::operator=(const Product&):
Product.h:20:   instantiated from void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Product, _Alloc = std::allocator<Product>]
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:610:   instantiated from âvoid std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = Product, _Alloc = std::allocator<Product>]â
HandleTCPClient.cpp:173:   instantiated from here
/usr/lib/qt-3.3/include/qobject.h:212: error: QObject& QObject::operator=(const QObject&) is private
Product.h:20: error: within this context
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc: In member function void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = Product, _Alloc = std::allocator<Product>]:
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:260: note: synthesized method âProduct& Product::operator=(const Product&) first required here 
make: *** [HandleTCPClient.o] Error 1

我的HandleTCPClient.cpp线路574的一部分:

代码语言:javascript
复制
            Product tempProduct;// temporary Product storage variable
            tempProduct.setHandler(this);
...

            else if (output[1] == '5') // description
            {
                output.erase(0,2); // erase the sequence numbers
                tempProduct.description = output;
    LINE 574            output = productDetails(tempProduct); // obtain client given information about selling product
            }

产品.h::

代码语言:javascript
复制
#include <string>
#include <qtimer.h>
#include "HandleTCPClient.h"
#ifndef PRODUCT_H
#define PRODUCT_H
#include <qobject.h>
#include <qgl.h>

class Handler;

//Define ourselves a product class
class Product : public QObject
    {

        Q_OBJECT

        void startTimer();

    public:
        Product();

        string seller, itemName, description, highestBidder;
        double price, min, buyingPrice, currentBid;
        int time;
        bool isSold;
        Handler *handler;

        void setHandler(Handler *h);

    public slots:
        void setProductToSold();

    };

#endif

Product.cpp::

代码语言:javascript
复制
#include <string>
using std::string;

#include "Product.h"

Product::Product()
{
    seller = "";
    itemName = "";
    price = 0.00;
    min = 0.00;
    buyingPrice = 0.00;
    time = 0;
    description = "";
    highestBidder = "None";
    currentBid = 0.00;
}

void Product::setHandler(Handler *h)
{
    handler = h;
}

谢谢你的帮助=)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2009-11-23 02:12:53

ProductQObject的子类,不能复制。您的代码试图将其复制到某个地方(可能是在productDetails(tempProduct)中),这会导致错误。也许你可以通过const引用将它传递给你的函数;或者也许需要重新设计你的程序。

您的编译器告诉您QObject的复制构造函数是私有的,因此它不能被任何不是基类方法的函数调用。Qt就是这样设计的。

Qt禁止复制QObjects的一个原因是它管理QObject的子代的内存。当一个QObject被删除时,它的所有子项也会被删除。如果QObject是可复制的,这将是不切实际的。

票数 23
EN

Stack Overflow用户

发布于 2009-11-23 02:12:07

QObject子体不允许复制...

请参阅http://lists.trolltech.com/qt-interest/2001-02/thread00123-0.html

您的productDetails()函数通过值接受其参数,因此需要进行一次复制。将其更改为采用常量引用。

票数 4
EN

Stack Overflow用户

发布于 2009-11-23 02:11:36

在第574行,您正在尝试将其中一项传递给productDetails函数。你没有显示它,但是我想这个函数要么接受一个值。所以编译器试图创建一个全新的对象来传递它,但这是库不允许的,它故意将复制构造函数设置为私有。

显式创建一个新对象,或者修复被调用的函数。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1779438

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档