[英]No match for call to struct sockaddr_in
I am trying to store the address info for each connection to a UDP server. 我正在尝试存储到UDP服务器的每个连接的地址信息。
I have a sockaddr_in struct for the client: 我为客户端有一个sockaddr_in结构:
struct sockaddr_in cli_data;
My goal is to store this struct each time a new client is connected into a vector of structs and use each element of the vector and therefore each respective struct element later on. 我的目标是每次将新客户端连接到结构的向量中时都存储此结构,并稍后使用该向量的每个元素以及每个相应的struct元素。
I am declaring the vector like that: 我在声明这样的向量:
std::vector<sockaddr_in> cli_addrlist;
I am then checking whether the recently connected address is present or not, and if it's not - I'm push_back
-ing the contents of cli_data to the cli_addrlist struct. 然后,我正在检查是否存在最近连接的地址,如果不存在-我在
push_back
将cli_data的内容添加到cli_addrlist结构中。 This is done by the following lines of code: 这是通过以下代码行完成的:
1. bool exists = false;
2. while ((n = recvfrom(s, buf, BUF_SIZE, 0, (struct sockaddr *) &cli_data, &len)) != -1)
3. {
4. for (int i = 0; i < cli_addrlist.size(); ++i)
5. {
6. if (inet_ntoa(cli_addrlist[i].sin_addr) == inet_ntoa(cli_data.sin_addr))
7. {
8. exists = true;
9. }
10. }
11. if (exists == false)
12. {
13. cli_addrlist.push_back(cli_data());
14. }
15. }
The error I'm getting from g++ is: 我从g ++得到的错误是:
error: no match for call to '(sockaddr_in) ()'
which is line 13. 这是第13行。
I might have missed something very simple, but I just don't seem to get my head around the issue. 我可能错过了一些非常简单的内容,但似乎并没有解决这个问题。
Thanks in advance. 提前致谢。
由于cli_data
是变量,因此您需要通过以下方式使用它:
cli_addrlist.push_back(cli_data); // No parenthesis
This 这个
cli_addrlist.push_back(cli_data());
should be 应该
cli_addrlist.push_back(cli_data);
Also, make sure this struct is suitable for pushing into vector. 另外,请确保此结构适合推入向量。 Like, if it has pointer members as its data members then it should have copy-constructor/assignment operator corresponding to that.
就像,如果它具有指针成员作为其数据成员,则它应该具有与之对应的复制构造函数/赋值运算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.