yat
yat

Reputation: 35

Undefined struct 'addrinfo' winsock2

I encountered an error and I didn't find any solution (even over the internet)

I created aQt 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.

Upvotes: 0

Views: 419

Answers (2)

3CxEZiVlQ
3CxEZiVlQ

Reputation: 38425

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

Upvotes: 1

SebDieBln
SebDieBln

Reputation: 3459

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.

Upvotes: 2

Related Questions