2009-11-22 13 views
8

i seguenti errori di compilazione è quello che ho:Errore con `QObject` sottoclasse e costruttore di copia:` QObject :: QObject (const QObject &) è 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 del mio HandleTCPClient.cpp Linea 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; 
} 

Grazie per tutto l'aiuto =)

risposta

21

Product è una sottoclasse di QObject, che non può essere copiata. Il tuo codice sta tentando di copiarlo da qualche parte (forse in productDetails(tempProduct)) e questo causa l'errore. Forse potresti passarlo alla tua funzione di riferimento const; o forse è necessario riprogettare il tuo programma.

Il compilatore sta dicendo che il costruttore di copia di QObject è privato, quindi non può essere chiamato da alcuna funzione che non sia un metodo della classe base. Qt l'ha progettato per funzionare in questo modo.

Una ragione per cui Qt disabilita la copia di QObject s è che gestisce la memoria dei figli di un QObject. Quando uno QObject viene eliminato, così fanno tutti i suoi figli. Questo sarebbe poco pratico se il QObject fosse riproducibile.

+0

Grazie per la spiegazione concisa - questo mi ha fatto risparmiare un sacco di tempo. (Stavo passando per valore, non riferimento in uno slot per quelli che arrivano a questo) –

2

on line 574 si sta tentando di passare uno di questi elementi alla funzione sui prodotti. Non lo mostri, ma immagino che questa funzione abbia un valore. Quindi il compilatore sta cercando di creare un oggetto nuovo di zecca per passarlo, ma ciò non è permesso dalla libreria, che ha intenzionalmente impostato il costruttore di copie come privato.

Creare un nuovo oggetto in modo esplicito o correggere la funzione chiamata.

Problemi correlati