【问题标题】:Error with `QObject` subclass and copy constructor: `QObject::QObject(const QObject&) is private``QObject` 子类和复制构造函数出错:`QObject::QObject(const QObject&) 是私有的`
【发布时间】:2010-12-19 06:19:02
【问题描述】:

我有以下编译错误:

/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 行的一部分:

            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
            }

Product.h::

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

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

感谢大家的帮助 =)

【问题讨论】:

    标签: c++ qt sockets networking


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      QObject 后代不允许复制...

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

      您的 productDetails() 函数按值获取其参数,因此需要进行复制。将其更改为采用 const 引用。

      【讨论】:

        【解决方案3】:

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

        您的编译器告诉您QObject 的复制构造函数是私有的,因此它不能被任何不是基类方法的函数调用。 Qt 将其设计为以这种方式工作。

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

        【讨论】:

        • 感谢您的简洁解释——这为我节省了很多时间。 (我是按值传递,而不是在插槽中引用)
        猜你喜欢
        • 2015-05-20
        • 1970-01-01
        • 1970-01-01
        • 2013-02-13
        • 1970-01-01
        • 2011-03-31
        • 1970-01-01
        • 2015-03-13
        • 1970-01-01
        相关资源
        最近更新 更多