C ++ overriding due to including header files multiple times

As the title says. I am facing override errors due to including header files multiple times. I know it because of this, but I don't know how to solve it. Yes, I previously posted the same issue on SO an hour in advance. But I couldn't explain correctly (I think so) and didn't get the expected answers. Here is the link:

C ++ Override Header Files

I am not editing this question as it was filled in :).

Ok, I have several classes and their structure looks like this:

main.cpp:

#include "Server.h"
#include "Handler.h"
#include "Processor.h"

int main(int argc, char* argv[])
{

}

      

server.h:

// Server.h
#pragma once

#include <winsock2.h>

      

Handler.h:

// Handler.h
#pragma once

#include <string>
#include <vector>

#include "Server.h"

      

Processor.cpp:

// Processor.cpp

#include "StdAfx.h"
#include "Processor.h"
#include "Handler.h"

      

server.cpp:

// Server.cpp

#include "Server.h"
#include "Processor.h"

      

The problem is that it <winsock2.h>

turns on several times, I don't know where it is. # pragma does the same task once as

#ifndef SOME_FILE_H
#define SOME_FILE_H
// code here
#endif // SOME_FILE_H

      

in my compiler (MSVC2008 in this case). So I'm pretty sure I don't need a heading that includes guards. But can you determine where I am making the mistake that <winsock2.>

turns on twice and how can I solve?

thank

+2


source to share


6 answers


In your project settings: Project Properties -> configuration -> advanced -> show includes.



It resets the header including the tree, from there you can see the culprit.

+5


source


You need some or all of them before enabling stdafx or windows.



#define _MSWSOCK_
#define NCB_INCLUDED
#define _WINSOCK2API_
#define _WINSOCKAPI_   /* Prevent inclusion of winsock.h in windows.h */

      

+1


source


I had the same problem lately and decided to enable it by turning it on winsock2.h

before turning it on windows.h

.

+1


source


Have you tried any suggestions we made in your other answer?

Seriously try using include guard instead #pragma once

.

If you still have an issue, then maybe go back and post another question on SO. Don't post multiple questions about the same thing because you don't want (or can't) take our advice!

0


source


try to replace

#include <winsock2.h>

      

from

#ifndef _WINSOCK2API_
#include <winsock2.h>
#endif

      

Since _WINSOCK2API_ is defined inside winsock2.h, the compiler will not try to include it multiple times.

0


source


I seem to remember this problem. I think there is a dependency issue between windows.h and winsock2.h I seem to remember going around it by always including windows.h to winsock2.h wherever it was used.

0


source







All Articles