c++ - 错误 `QObject` 子类和复制构造函数 : `QObject::QObject(const QObject&) is private`

标签 c++ qt sockets networking

以下是我遇到的编译错误:

/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

我的 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
            }

产品.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

产品.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;
}

感谢所有的帮助 =)

最佳答案

ProductQObject的子类,不可复制。您的代码试图将其复制到某处(可能在 productDetails(tempProduct) 中),这会导致错误。也许您可以通过 const 引用将它传递给您的函数;或者可能需要对您的程序进行一些重新设计。

你的编译器告诉你 QObject 的复制构造函数是私有(private)的,所以它不能被任何不是基类方法的函数调用。 Qt 将其设计为以这种方式工作。

Qt 禁止复制 QObject 的一个原因是它管理着 QObject 的子对象的内存。当一个 QObject 被删除时,它的所有子对象也会被删除。如果 QObject 是可复制的,这将无法正确执行。

关于c++ - 错误 `QObject` 子类和复制构造函数 : `QObject::QObject(const QObject&) is private` ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1779438/

相关文章:

iphone - 如何使用 CocoaAsyncSocket 读取数据?

python - 非常简单的 python 客户端/服务器工作,但奇怪的延迟

java - 是否有可能在 Android 蓝牙和笔记本电脑蓝牙设备之间建立不安全的连接?

qt - QSignalMapper 可以用来重新发出带有多个参数的信号吗?

c++ - Qt 5.0.2 和 OpenGL 3+ 渲染问题

c++ - Qt QML - 无法分配给不存在的属性 "onClicked"

c++ - 可以安全地存储 list::iterator 供以后使用吗?

java - 未使用 `nar-maven-plugin` 编译的 JUnit 测试的 native 代码

C++有什么方法可以在数字的开头添加小数点?

qt - 如何将键盘光标/焦点移动到 QLineEdit?