【发布时间】:2017-01-23 15:26:21
【问题描述】:
关于:
p.s.我知道这是一个具体的问题,但我很茫然,不知道为什么会这样
下面的算法应该将结构添加到列表中。每个结构都是一个server_connection 对象,接收列表包含在另一个名为VPN_Server 的结构中。
将结构添加到列表时,它只保存添加的第一个结构,不再添加。
调试器窗口确认了这一点:
对于每个新 IP,都会创建一个新的 VPN_Server 结构,并创建一个新的 server_connection 并将 push_back'ed 到 ServerInstancesList 列表中。但是在 foreach 循环中,尝试添加对象是没有结果的。
问题:
当将 server_connection 对象推送到特定的 VPN_Server 结构时,它会将这些数据保存在临时 foreach 容器中,但不会应用它。
我试过了:
-
将自定义
addConnection()方法添加到VPN_Serverstructvoid addConnection(server_connection s_con){ ServerInstancesList.push_back(s_con); }
创建一个临时列表,添加 server_connection 并创建一个新的 VPN_server 对象,并将其设置为等于当前的 foreach 容器。
这些都没有帮助。
测试和深入描述:
在算法中,我的第一个和第三个vpn_connectionstruct拥有相同的IP。在第三次迭代中,执行 foreach 循环并发生以下情况。
VPN_Server ser 包含第一个结构信息的信息,即一个 IP 和一个在其 QList<server_connection> 中称为 ServerInstancesList 的对象。
ser 通过addConnection(s_con) 添加了一个对象。之后,循环以返回终止。但是ser 注册了添加的对象,而在 foreach 循环之外,没有添加新对象。甚至对列表中的任何结构都没有。
这似乎很容易解决,但我找不到它,
我们将不胜感激!
代码:
server_connection
struct server_connection{
//Is a connection contained by IP
QString cipher,protocol;
int port;
bool lzo_compression;
server_connection(){}
server_connection(QString _cipher, QString _protocol, int _port, bool _comp){
cipher = _cipher;
protocol = _protocol;
port = _port;
lzo_compression = _comp;
}
};
VPN_Server
struct VPN_Server{
//Holds IP as sort value and list of connection info
QString VPN_IP;
QList<server_connection> ServerInstancesList;
VPN_Server(){
ServerInstancesList = QList<server_connection>();
}
VPN_Server(QString ip, QList<server_connection> server_con_list){
VPN_IP = ip;
ServerInstancesList = server_con_list;
}
void addConnection(server_connection s_con){
ServerInstancesList.push_back(s_con);
}
};
算法:
QList<VPN_Server> data_mananger::parseVPNConnections(QList<VPNConnection> l){
//Init var
QList<VPN_Server> server_list = QList<VPN_Server>();
VPNConnection v_con;
bool bAdded = false;
server_connection s_con;
//processing all vpn_connections, this is raw form sent, contains as fields, ip, cipher, protocol, port, compression
foreach (v_con, l) {
//create server_connection - data sorted by ip
s_con = server_connection(v_con.cipher, v_con.protocol, v_con.port, v_con.lzo_compression);
//pass through existing data, checking for a matching ip
foreach (VPN_Server ser, server_list) {
if (ser.VPN_IP == v_con.ip) {
//ip match found -> there already exists and ip with a server connection, adding another one with s_con
ser.addConnection(s_con);
bAdded = true;
//break current loop short searching for a matching ip
break;
}
}
//bAdded = false -> no matching IP has been found, thus creating a nw VPNServer
if (!bAdded) {
VPN_Server serv;
//creating new connection list and adding s_con to this list
QList<server_connection> list = QList<server_connection>();
list.push_back(s_con);
//adding VPNServer to list containing VPNServers
serv = VPN_Server(v_con.ip, list);
server_list.push_back(serv);
}
else
//data has been added, reseting add flag
bAdded = false;
}
return server_list;
}
【问题讨论】:
-
嗯,在您的
foreach (VPN_Server ser, server_list)中您调用ser.addConnection(s_con);- 这不是对临时(复制)对象的调用吗?看起来循环应该改为foreach (VPN_Server& ser, server_list) -
@YuriyIvaskevych 我试过了。构建错误:
qglobal.h:963: error: binding ‘const Misc::VPN_Server’ to reference of type ‘Misc::VPN_Server&’ discards qualifiers for (variable = *_container_.i; _container_.control; _container_.control = 0) -
哎呀,Qt 的
foreach确实不允许修改(请看here)。尝试使用简单的for -
@YuriyIvaskevych 你是救世主。非常感谢!