我已经在这几个小时了,我只是想弄明白,有人可以帮助我吗?
cout << "Please enter the port to listen on: ";
int port = 2232;
cin >> port;
cout << "Please enter your ip address: ";
string addr;
cin >> addr; //ERROR 1
puts("");
WSAData wsa;
WORD Version = MAKEWORD(2,1);
WSAStartup(Version, &wsa);
SOCKET Listen = socket(AF_INET, SOCK_STREAM, NULL);
SOCKET Connect = socket(AF_INET, SOCK_STREAM, NULL);
SOCKADDR_IN Server;
Server.sin_addr.s_addr = inet_addr(addr); //ERROR 2
1错误C2040:'ip':'const char *'与'std :: string'的间接等级不同
2 IntelliSense:从“std :: string”到“const char *”不存在合适的转换函数
答案 0 :(得分:1)
这是因为inet_addr
需要const char*
,而您传递std::string
。致电c_str()
进行转换,如下所示:
Server.sin_addr.s_addr = inet_addr(addr.c_str());
第二条错误消息只是告诉您标准库无法隐式执行此转换。