最后编辑:
事实证明,除了我应该编写ui->lineEdit_decviceName而不是ui->label_decviceName,之外,没有什么问题,否则我将永远得不到正确的输入。正确输入的检查在这个函数之外,所以我几乎进入了无限循环。每个人都应该注意到错误,这可能会伤害你。
因此,我尝试使用Qt4.8、QtCreator和第三方库(这个库很好)实现登录进程。
在主程序中,我定义了一个类的指针:
DeviceData* m_gcurrentdevicedata;并将其传递给Login对象:
m_loginandadddevice = new LoginAndAddDevice(..., m_gcurrentdevicedata, ...);此Login类的构造函数定义:
LoginAndAddDevice(..., DeviceData* currentdevicedata, ....);在Login类的构造函数中,我将这个指针传递给相同类型的另一个指针:
this->logincurrentdevicedata = currentdevicedata;之后,我给这个指针的对象赋值:
logincurrentdevicedata = &(*it); ('it' is an iterator)因此,现在我假设主程序中的变量gcurrentdevicedata在Login对象中具有相同的地址和logincurrentdevicedata值。(并且我可以从logincurrentdevicedata中获得值而不会出错)
然而,当我试图从主程序中获得gcurrentdevicedata的价值时,整个程序就会崩溃。我只能猜测指针logincurrentdevicedata与gcurrentdevicedata.没有相同的地址或值但我为什么以及如何克服它呢?在此之前,非常感谢您。
更多详情:
void LoginAndAddDevice::on_pushButton_login_clicked()
{
QString name = ui->label_decviceName->text().trimmed();
QList<DeviceData>& listDeviceDataref = *devicedatalist; //assign this pointer to a reference
int & useridref = *userID; //assign this pointer to a reference
QList<DeviceData>::iterator it;
for(it = listDeviceDataref.begin();it!=listDeviceDataref.end();++it)
{
if(((*it).getDeviceName() == name) && !name.isEmpty())
{
break;
}
}
qDebug()<<"after login button pressed "+QString::number((*it).getUsrID());
DeviceData& devicedataref = (*it); //here I assign the pointer value to a reference value
if(devicedataref.m_iuserid>=0)
{
qDebug()<<"after login button pressed, before set the value "+QString::number((*it).getUsrID());
useridref = devicedataref.getUsrID();
qDebug()<<"after login button pressed, before set the deviceinfo value "+QString::number(*userID);
m_logindeviceinfo = &(devicedataref.m_deviceinfo);
qDebug()<<"after login button pressed, before set the currentdevicedata value "+QString::number(*userID);
*logincurrentdevicedata = devicedataref; //this line leads to crash, prior code works fine
qDebug()<<"12121";
qDebug()<<"logincurrentdevicedata's user id: "+ QString::number(logincurrentdevicedata->getUsrID());
ui->pushButton_cancel->setText(tr("Ok"));
return;
}
//...
}发布于 2016-01-13 03:57:56
在像STL这样的现代C++库中,以下内容既不能保证获得地址,也不能保证安全:
logincurrentdevicedata = &(*it); // ('it' is an iterator)使用此表达式,您有可能获得临时地址或集合删除/移动项的地址。但是是的,有些容器实现/用例可能以这种方式获得集合项的地址:同样,集合需要保持不变,而项目则被消耗。
虽然有这样的可能性,但如果不知道迭代器和容器类型的具体情况,我们就不能肯定地说。那个容器的生命周期都不清楚。您还需要确保迭代器是有效的,并且不指向其集合的末尾。
相反,可以创建该对象的副本:
auto it = collection.begin(); // OR can be an arbitrary iterator
if (it != collection.end())
auto val = *it; // make sure that new object is available
// if you pass an address of it to somewhere.也可以将对该集合的引用传递给使用者:
void consumer(std::list<T>& collection)
{
auto it = collection.begin(); // OR can be an arbitrary iterator
if (it != collection.end())
it->access(); // now it supposed to be safe
}P.S.在该问题作者之后提供了一个示例,结果表明,该变量实际上是一个指针,并且赋值指向它所指向的实际值:
*logincurrentdevicedata = devicedataref; // copying an object here在这里,您“取消引用”指针,并使其指向对象,并按值进行赋值。代码仍然不够,因为不清楚logincurrentdevicedata指针是否包含DeviceData对象的真实地址。
为了使它发挥作用,它必须满足:
DeviceData deviceData;
DeviceData* logincurrentdevicedata = &deviceData;或者:
DeviceData* logincurrentdevicedata = new DeviceData;问题似乎与基本C有关,从这一点出发,我们无法确定代码中其他可能不正确的地方。我在代码中看到了其他几个疑点,但是这样的问题可能只对https://codereview.stackexchange.com/有好处。
https://stackoverflow.com/questions/34757523
复制相似问题