Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

Thanks for all the help =)

like image 838
Aero Chocolate Avatar asked Nov 22 '09 18:11

Aero Chocolate


People also ask

What is QObject C++?

QObject is the heart of the Qt Object Model. The central feature in this model is a very powerful mechanism for seamless object communication called signals and slots. You can connect a signal to a slot with connect() and destroy the connection with disconnect().

Is QObject copyable?

There are several reasons why a QObject can't be copied. The two biggest reasons are: QObjects usually communicate with each other using the signals and slots mechanism. It's unclear whether the connected signals and/or slots should be transferred over to the copy.

What are copy constructors in C++?

Copy Constructor in C++ A copy constructor is a member function that initializes an object using another object of the same class. In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor.

Is QWidget a QObject?

A QWidget is derived of QObject .


2 Answers

Product is a subclass of QObject, which cannot be copied. Your code is attempting to copy it somewhere (perhaps in productDetails(tempProduct)) and this causes the error. Perhaps you could pass it to your function by const reference instead; 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. Qt has designed it to work that way.

One reason that Qt disables copying of QObjects is that it manages the memory of the children of a QObject. When a QObject gets deleted, so do all of its children. This would be impractical to do properly if the QObject was copyable.

like image 73
int3 Avatar answered Oct 16 '22 15:10

int3


Copying is not allowed for QObject descendants...

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

Your productDetails() function takes its parameter by value, making a copy necessary. Change it to take a const reference instead.

like image 38
rep_movsd Avatar answered Oct 16 '22 16:10

rep_movsd