错误与`QObject`子类和复制构造函数:`QObject :: QObject(const QObject&)是private' [英] Error with `QObject` subclass and copy constructor: `QObject::QObject(const QObject&) is private`

查看:2066
本文介绍了错误与`QObject`子类和复制构造函数:`QObject :: QObject(const QObject&)是private'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  /usr/lib/qt-3.3/include/qobject.h :复制构造函数Product :: Product(const Product&):
/usr/lib/qt-3.3/include/qobject.h:211:error:QObject :: QObject(const QObject&)is private
Product.h:20:error:在这个上下文中
HandleTCPClient.cpp:在成员函数中int Handler :: HandleTCPClient(int,std :: string,std :: string):
HandleTCPClient.cpp :574:注意:合成方法产品::产品(const Product&)首先需要这里
HandleTCPClient.cpp:574:错误:初始化std :: string的参数1 productDetails(Product)
/ usr / lib / qt-3.3 / include / qobject.h:在成员函数Product& Product :: operator =(const Product&):
Product.h:20:从void std :: vector< _Tp,_Alloc> :: _ M_insert_aux(__ gnu_cxx :: __ normal_iterator< typename std :: _ Vector_base& _Alloc> :: _Tp_alloc_type :: pointer,std :: vector< _Tp,_Alloc>>,const _Tp&)[with _Tp = Product,_Alloc = std :: allocator< Product>]
/ usr / gcc / i386-redhat-linux / 4.1.2 /../../../../include / c ++ / 4.1.2 / bits / stl_vector.h:610:从âvoidstd :: vector< _Tp实例化, _Alloc> :: push_back(const _Tp&)[with _Tp = Product,_Alloc = std :: allocator< Product>]?
HandleTCPClient.cpp:173:从这里实例化
/ usr / lib / qt -3.3 / include / qobject.h:212:error:QObject& QObject :: operator =(const QObject&)is private
Product.h:20:error:在此上下文中
/usr/lib/gcc/i386-redhat-linux/4.1.2/ .. /../../../include/c++/4.1.2/bits/vector.tcc:在成员函数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:合成方法; Product :: operator =(const Product&)首先需要这里
make:*** [HandleTCPClient.o]错误1

我的HandleTCPClient.cpp的一部分571行:

 产品tempProduct; //临时产品存储变量
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); //获取客户端有关销售产品的信息
}

Product.h ::



  #include< string> 
#include< qtimer.h>
#includeHandleTCPClient.h
#ifndef PRODUCT_H
#define PRODUCT_H
#include< qobject.h>
#include< qgl.h>

类Handler;

//定义一个产品类
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;

#includeProduct.h

Product :: Product()
{
seller =;
itemName =;
price = 0.00;
min = 0.00;
purchasePrice = 0.00;
time = 0;
description =;
highestBidder =无;
currentBid = 0.00;
}

void Product :: setHandler(Handler * h)
{
handler = h;感谢所有的帮助=)


}
c> c> QObject 的子类无法复制。您的代码试图将其复制到某处(可能在 productDetails(tempProduct)),这将导致错误。也许你可以通过const引用传递给你的函数;



你的编译器告诉你 QObject 的复制构造函数是private,所以它不能被任何不是基类的方法的函数调用。



Qt禁止复制 QObject 的一个原因是它管理一个 QObject 的子节点的内存。当 QObject 被删除时,它的所有子项也被删除。如果 QObject 是可复制的,这样做是不切实际的。


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 =)

解决方案

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.

这篇关于错误与`QObject`子类和复制构造函数:`QObject :: QObject(const QObject&amp;)是private'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆