简体   繁体   English

未定义的结构'addrinfo'winsock2

[英]Undefined struct 'addrinfo' winsock2

I encountered an error and I didn't find any solution (even over the internet)我遇到了一个错误,我没有找到任何解决方案(即使是通过互联网)

I created a Qt app to receive data using a TCP protocol and plot them using QcustomPlot .我创建了一个Qt应用程序来使用TCP协议和 plot 它们使用QcustomPlot接收数据。

I have the following files:我有以下文件:

mainwindow.h :主窗口.h

#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_mainwindow.h"
#include <QVector>
#include <iostream>

class MainWindow: public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = Q_NULLPTR);
    // ...

private:
    struct addrinfo _hints;
    struct addrinfo* _result = NULL;
    // ...
};

mainwindow.cpp :主窗口.cpp

#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>

#pragma comment(lib, "ws2_32.lib")

#include "mainwindow.h"
#include <QVector>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
    //...
}

and the main.cpp file:main.cpp文件:

#include "mainwindow.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

I have the following error: 'MainWindow::hints' uses undefined struct 'addrinfo' (compiling source file main.cpp).我有以下错误:“MainWindow::hints”使用未定义的结构“addrinfo”(编译源文件 main.cpp)。

I don't understand why I have this error, because I tested the same program in a classic consol app following the microsoft tutorial and it was working.我不明白为什么会出现此错误,因为我按照microsoft 教程在经典的 consol 应用程序中测试了相同的程序并且它正在运行。 I believe it comes from the includes, but I still dont have a clue about which one causes that.我相信它来自包含,但我仍然不知道是哪一个导致了这种情况。

You need to #include something in your mainwindow.h that defines struct addrinfo , because your MainWindow class has a member variable of that type.您需要在定义struct addrinfo#includemainwindow.h一些内容,因为您的MainWindow class 具有该类型的成员变量。 At the moment you include all the socket stuff only in your *.cpp file.目前,您仅在*.cpp文件中包含所有套接字内容。

You use #define WIN32_LEAN_AND_MEAN that prevents including many dependent header files and makes you include required header files explicitly.您使用#define WIN32_LEAN_AND_MEAN来防止包含许多相关的 header 文件,并使您明确包含所需的 header 文件。 As for addrinfo you have to #include <ws2def.h> .至于addrinfo你必须#include <ws2def.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM