Click here to Skip to main content
15,896,315 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Hi
I have a problem when compiling a cirtain .cpp file which needs 2 includes, say:

#include "file1.h"
#include "file2.h"

When putting them in this order it compiles just fine. When I change their order, I get a lot of compilation errors like this:

...\winsock2.h(1762) : error C2653: 'System' : is not a class or a namespace name
...\winsock2.h(1762) : error C2065: 'Int16' : undeclared identifier
...\winsock2.h(1762) : error C2144: syntax error : 'u_short' should be preceded by ')'
...\winsock2.h(1762) : error C2448: 'HostToNetworkOrder' : function-style initializer appears to be a function definition
...\winsock2.h(1762) : error C2059: syntax error : ')'

These errors repeat for line (1850) in winsock2.h.
When I go to line (1762) in file winsock2.h I don't see any of the above names...

Anyone has an idea?
Posted

You missunderstood the meaning of "include". This is like copy&paste with sharing code resources.
For example:

// file1.h
#define SUCCESSOR(X)		X+1

// file2.h
#define	INCREMENT(X)		X = SUCCESSOR(X)

// yourcode.cpp

#include "file1.h"
#include "file2.h"

void main()
{
	int		value = 5;
	INCREMENT(value);
}

that looks like:
// yourcode.cpp

#define SUCCESSOR(X)		X+1
#define	INCREMENT(X)		X = SUCCESSOR(X)

void main()
{
	int		value = 5;
	INCREMENT(value);
}

in other order:
// yourcode.cpp

#include "file2.h"
#include "file1.h"

void main()
{
	int		value = 5;
	INCREMENT(value);
}


yourcode.cpp
// yourcode.cpp

#define	INCREMENT(X)		X = SUCCESSOR(X) // <- 
// compiler throws a unknown symbol error for 'SUCCESSOR'

#define SUCCESSOR(X)		X+1

void main()
{
	int		value = 5;
	INCREMENT(value);
}


but you can do it also in this way:
// file2.h
#include "file1.h"
#define	INCREMENT(X)		X = SUCCESSOR(X)

in this case you have to prevent the compiler to include the symbols twice.

// yourcode.cpp

#include "file1.h"
#include "file2.h"

void main()
{
	int		value = 5;
	INCREMENT(value);
}


yourcode.cpp
// yourcode.cpp

// #include "file1.h"
#define SUCCESSOR(X)		X+1

// #include "file2.h"
#define SUCCESSOR(X)		X+1 // <- compiler throws a redefined symbol error
#define	INCREMENT(X)		X = SUCCESSOR(X)

void main()
{
	int		value = 5;
	INCREMENT(value);
}


how to prevent that?
// file1.h
#ifndef __FILE1_H_INCLUDED
#define __FILE1_H_INCLUDED

#define SUCCESSOR(X)		X+1

#endif // __FILE1_H_INCLUDED

in your code
// yourcode.cpp

// #include "file1.h"
#ifndef __FILE1_H_INCLUDED
#define __FILE1_H_INCLUDED

#define SUCCESSOR(X)		X+1

#endif // __FILE1_H_INCLUDED

// #include "file2.h"
#ifndef __FILE1_H_INCLUDED // <- compiler ignores code inside this section
#define __FILE1_H_INCLUDED

#define SUCCESSOR(X)		X+1

#endif // __FILE1_H_INCLUDED
#define	INCREMENT(X)		X = SUCCESSOR(X)

void main()
{
	int		value = 5;
	INCREMENT(value);
}

Regards.
 
Share this answer
 
Comments
Olivier Levrey 31-Mar-11 7:35am    
Good explanations. My 5.
This is not include… you this is .NET referenced assemblies and name spaces.
It looks like the code is not yours. You probably don't know the platform and language. Your tag says "C++", but the code is not "C++", but "C++/CLI", at least part of it. I have no idea where you where you get u_short, but everything else is from .NET. It has nothing to do with winsock2.h. You need to use .NET name spaces System and System::Net.

By the way, don't you think it's not very nice to show compilation errors and not showing respective code lines?

So, it's hard to say how anyone can help you. To understand anything you need to start writing your code, much more simple code, instead of trying to compile something you have no idea about.
Maybe, you need to start from the very beginning: How to create a MIDI file with fixed-size?[^], http://en.wikipedia.org/wiki/C%2B%2B/CLI[^]…

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900