while (count < bluetoothCount) { WSAQUERYSET result; memset(&result, 0, sizeof(result)); result.dwSize = sizeof(result); iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &result); if (iResult != 0) { break; } count++; } &result报错,实参与形参不兼容 怎么修改
时间: 2023-06-17 14:08:29 浏览: 27
根据你提供的代码,我们可以看出 `WSAQUERYSET result` 是在 while 循环内部定义的,因此在 while 循环外部是无法访问到 `result` 的。所以,你可能需要将 `WSAQUERYSET result` 定义在 while 循环外部,以便在循环结束后访问它。
另外,根据 MSDN 中对 `WSALookupServiceNext` 函数的描述,该函数的第二个参数应该是一个指向 `WSAQUERYSET` 结构体的指针,而你的代码中传入的是 `&result`,即指向 `WSAQUERYSET` 结构体的指针的指针,这可能会导致编译器报错。因此,你需要将调用 `WSALookupServiceNext` 函数时的第二个参数修改为 `&result`。
综上所述,你可以尝试将代码修改为如下形式:
```
WSAQUERYSET result;
memset(&result, 0, sizeof(result));
result.dwSize = sizeof(result);
int count = 0;
while (count < bluetoothCount) {
int iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &result);
if (iResult != 0) {
break;
}
count++;
}
// 在这里访问 result
```
相关问题
while (count < bluetoothCount) { WSAQUERYSET result; memset(&result, 0, sizeof(result)); result.dwSize = sizeof(result); iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &result); if (iResult != 0) { break; } count++; } &result报错,实参与形参不兼容
根据你提供的代码,问题出在 `&result` 这个参数上。`WSALookupServiceNext` 函数的第三个参数是一个指向 `WSAQUERYSET` 结构体的指针,而你的代码中使用了 `&result`,这是一个指向 `WSAQUERYSET` 结构体的指针的指针,所以会出现类型不兼容的错误。
解决方法是直接使用 `result`,而不是 `&result`。修改后的代码如下:
```c++
while (count < bluetoothCount) {
WSAQUERYSET result;
memset(&result, 0, sizeof(result));
result.dwSize = sizeof(result);
iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &result);
if (iResult != 0) {
break;
}
count++;
}
```
这样就可以避免类型不兼容的错误了。
int main() { std::map<std::string, std::string> config = readIniFile("config.ini"); int bluetoothCount = std::stoi(config["bluetooth.count"]); std::string logFileName = config["log.filename"]; WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { std::cout << "WSAStartup failed: " << iResult << std::endl; return 1; } WSAQUERYSET service; memset(&service, 0, sizeof(service)); service.dwSize = sizeof(service); service.dwNameSpace = NS_BTH; HANDLE lookupHandle = NULL; iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle); if (iResult != 0) { std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl; WSACleanup(); return 1; } int count = 0; while (count < bluetoothCount) { WSAQUERYSET result; memset(&result, 0, sizeof(result)); result.dwSize = sizeof(result); iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &result); if (iResult != 0) { break; } count++; } WSALookupServiceEnd(lookupHandle); WSACleanup(); if (count >= bluetoothCount) { std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } else { std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } return 0; } &result报错,实参与形参不兼容 优化一下好吗
可以尝试将WSAQUERYSET result改为指针类型WSAQUERYSET* result,修改后的代码如下:
```
int main() {
std::map<std::string, std::string> config = readIniFile("config.ini");
int bluetoothCount = std::stoi(config["bluetooth.count"]);
std::string logFileName = config["log.filename"];
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cout << "WSAStartup failed: " << iResult << std::endl;
return 1;
}
WSAQUERYSET service;
memset(&service, 0, sizeof(service));
service.dwSize = sizeof(service);
service.dwNameSpace = NS_BTH;
HANDLE lookupHandle = NULL;
iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle);
if (iResult != 0) {
std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl;
WSACleanup();
return 1;
}
int count = 0;
while (count < bluetoothCount) {
WSAQUERYSET* result = new WSAQUERYSET;
memset(result, 0, sizeof(WSAQUERYSET));
result->dwSize = sizeof(WSAQUERYSET);
iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, result);
if (iResult != 0) {
break;
}
count++;
delete result;
}
WSALookupServiceEnd(lookupHandle);
WSACleanup();
if (count >= bluetoothCount) {
std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount);
writeLogFile(logFileName, logText);
} else {
std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount);
writeLogFile(logFileName, logText);
}
return 0;
}
```
这样就可以正常编译运行了。另外,需要注意在while循环中对WSAQUERYSET* result的内存分配和释放。