简体   繁体   English

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

[英]Error with `QObject` subclass and copy constructor: `QObject::QObject(const QObject&) is private`

The following compile errors is what I have: 我有以下编译错误:

/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

Part of my HandleTCPClient.cpp Line 574: 我的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:: 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:: 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;
}

Thanks for all the help =) 谢谢你的帮助=)

Product is a subclass of QObject , which cannot be copied. ProductQObject的子类,无法复制。 Your code is attempting to copy it somewhere (perhaps in productDetails(tempProduct) ) and this causes the error. 您的代码正在尝试将其复制到某处(可能在productDetails(tempProduct) ),这会导致错误。 Perhaps you could pass it to your function by const reference instead; 也许你可以通过const引用将它传递给你的函数; or perhaps some redesign of your program is needed. 或者可能需要对您的程序进行一些重新设计。

Your compiler is telling you that the copy constructor of QObject is private, so it cannot be called by any function that is not a method of the base class. 您的编译器告诉您QObject的复制构造函数是私有的,因此任何不是基类方法的函数都不能调用它。 Qt has designed it to work that way. Qt设计它以这种方式工作。

One reason that Qt disables copying of QObject s is that it manages the memory of the children of a QObject . Qt禁用QObject复制的一个原因是它管理QObject子代的内存。 When a QObject gets deleted, so do all of its children. QObject被删除时,它的所有子QObject被删除。 This would be impractical to do properly if the QObject was copyable. 如果QObject是可复制的,那么这样做是不切实际的。

Copying is not allowed for QObject descendants... QObject后代不允许复制......

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

Your productDetails() function takes its parameter by value, making a copy necessary. productDetails()函数按值获取其参数,从而需要复制。 Change it to take a const reference instead. 将其更改为采用const引用。

On line 574 you are trying to pass one of these items to the productDetails function. 在第574行,您尝试将其中一个项目传递给productDetails函数。 You don't show it, but I imagine that this function either takes a value. 你没有展示它,但我想这个函数要么取值。 So the compiler is trying to create a brand new object to pass it, but that's not permitted by the library, which has intentionally set the copy constructor to be private. 因此编译器正在尝试创建一个全新的对象来传递它,但是库不允许这样做,因为它故意将复制构造函数设置为私有。

Make a new object explicitly, or fix the called function. 显式创建一个新对象,或修复被调用的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM