2009-11-22 32 views
8

Sau đây biên dịch lỗi là những gì tôi có:Lỗi với `QObject` lớp con và copy constructor:` QObject :: QObject (const QObject &) là 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 

Một phần của dòng HandleTCPClient.cpp tôi 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ảm ơn tất cả sự giúp đỡ =)

Trả lời

21

Product là lớp con của QObject, không thể sao chép được. Mã của bạn đang cố gắng sao chép nó ở đâu đó (có lẽ trong số productDetails(tempProduct)) và điều này gây ra lỗi. Có lẽ bạn có thể chuyển nó vào hàm của bạn bằng tham chiếu const thay vào đó; hoặc có lẽ một số thiết kế lại chương trình của bạn là cần thiết.

Trình biên dịch của bạn cho bạn biết rằng hàm tạo bản sao của QObject là riêng tư, vì vậy nó không thể được gọi bởi bất kỳ hàm nào không phải là phương thức của lớp cơ sở. Qt đã thiết kế nó để hoạt động theo cách đó.

Một lý do khiến Qt vô hiệu hóa việc sao chép QObject s là nó quản lý bộ nhớ của trẻ em trong số QObject. Khi một số QObject bị xóa, hãy thực hiện tất cả các con của nó. Điều này sẽ không thực tế nếu bạn có thể sao chép QObject.

+0

Cảm ơn bạn đã giải thích ngắn gọn - điều này vừa tiết kiệm cho tôi rất nhiều thời gian. (Tôi đã đi qua giá trị, không tham chiếu trong một khe cắm cho những người đến này) –

2

On line 574 bạn đang cố gắng để vượt qua một trong những mục vào productDetails chức năng. Bạn không hiển thị nó, nhưng tôi tưởng tượng rằng hàm này có giá trị. Vì vậy, trình biên dịch đang cố gắng tạo ra một đối tượng hoàn toàn mới để vượt qua nó, nhưng điều đó không được thư viện cho phép, điều này đã cố tình thiết lập hàm tạo bản sao là riêng tư.

Tạo đối tượng mới một cách rõ ràng hoặc sửa chức năng được gọi.

Các vấn đề liên quan