简体   繁体   中英

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 .

I have the following files:

mainwindow.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 :

#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:

#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).

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. 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. At the moment you include all the socket stuff only in your *.cpp file.

You use #define WIN32_LEAN_AND_MEAN that prevents including many dependent header files and makes you include required header files explicitly. As for addrinfo you have to #include <ws2def.h> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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