C++ `QObject`子类和复制构造函数出错:`QObject::QObject(const QObject&;)是私有的`
以下是我遇到的编译错误:C++ `QObject`子类和复制构造函数出错:`QObject::QObject(const QObject&;)是私有的`,c++,qt,sockets,networking,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 HandleTCP
/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
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
#包括
#包括
#包括“HandleTCPClient.h”
#ifndef产品
#定义产品
#包括
#包括
类处理程序;
//定义我们自己的产品类
类别产品:公共QObject
{
Q_对象
void startTimer();
公众:
产品();
字符串卖方、项目名称、说明、最高投标人;
双倍价格、最低价格、买入价格、当前出价;
整数时间;
布尔·伊索尔德;
处理者*处理者;
void setHandler(Handler*h);
公众时段:
void setProductToSold();
};
#恩迪夫
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;
}
#包括
使用std::string;
#包括“Product.h”
Product::Product()
{
卖方=”;
itemName=“”;
价格=0.00;
最小值=0.00;
购买价格=0.00;
时间=0;
description=“”;
最高投标人=“无”;
当前投标=0.00;
}
void Product::setHandler(Handler*h)
{
handler=h;
}
感谢您在第574行提供的所有帮助=)您正试图将其中一项传递给productDetails函数。您没有显示它,但我认为这个函数要么接受一个值。因此,编译器试图创建一个全新的对象来传递它,但是库不允许这样做,它故意将复制构造函数设置为私有
显式创建新对象,或修复调用的函数。不允许对QObject子体进行复制 看
productDetails()函数按值获取其参数,因此需要复制。将其更改为采用常量引用。
Product
是QObject
的子类,无法复制。您的代码试图将其复制到某个地方(可能在productDetails(tempProduct)
中),这会导致错误。也许您可以通过const引用将其传递给函数;或者可能需要重新设计您的程序
您的编译器告诉您,QObject
的复制构造函数是私有的,因此它不能被任何不是基类方法的函数调用。Qt设计它就是这样工作的
Qt禁用
QObject
s复制的一个原因是它管理QObject
的子对象的内存。当一个QObject
被删除时,它的所有子对象也会被删除。如果QObject
是可复制的,那么这样做是不切实际的。谢谢你的简明解释——这为我节省了很多时间。(我是通过值传递的,而不是通过插槽中的引用传递的)
#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;
}