2009-11-22 9 views
8

los siguientes errores de compilación es lo que tengo:error con `QObject` subclase y constructor de copia:` QObject :: QObject (const QObject y) es private`

/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 

Parte de mi línea 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; 
} 

Gracias por toda la ayuda =)

Respuesta

21

Product es una subclase de QObject, que no se puede copiar. Su código está intentando copiarlo en algún lugar (quizás en productDetails(tempProduct)) y esto causa el error. Tal vez podría pasarlo a su función por referencia constante; o tal vez sea necesario rediseñar su programa.

Su compilador le dice que el constructor de copia de QObject es privado, por lo que no puede ser llamado por ninguna función que no sea un método de la clase base. Qt lo ha diseñado para que funcione de esa manera.

Una razón por la que Qt desactiva la copia de QObject s es que administra la memoria de los hijos de QObject. Cuando se elimina un QObject, también lo hacen todos sus hijos. Esto no sería práctico si se puede copiar el QObject.

+0

Gracias por la explicación concisa, esto me ha ahorrado mucho tiempo. (Estaba pasando por valor, no referencia en una ranura para los que vienen a esto) –

2

En la línea 574 que están tratando de pasar uno de estos elementos a la función Detalles del producto. No lo muestras, pero imagino que esta función toma un valor. Entonces, el compilador intenta crear un nuevo objeto para pasarlo, pero eso no está permitido por la biblioteca, que ha establecido intencionalmente que el constructor de copia sea privado.

Cree un nuevo objeto explícitamente, o corrija la función llamada.

Cuestiones relacionadas