简体   繁体   English

编译器错误C2275

[英]Compiler error C2275

I've been programming a simple WinSock application in Visual Studio 2010. I have named my application entry point "main.c", then I came across this error while declaring a SOCKET object: 我在Visual Studio 2010中编写了一个简单的WinSock应用程序。我将我的应用程序入口点命名为“main.c”,然后在声明SOCKET对象时遇到了这个错误:

error C2275: 'SOCKET' : illegal use of this type as an expression

Oddly enough, I solved that problem by renaming the code file from main.c to main.cpp 奇怪的是,我通过将代码文件从main.c重命名为main.cpp来解决了这个问题

Just out of curiosity, I want to know what is the meaning of this error, and what difference occurred by changing the extension. 出于好奇,我想知道这个错误的含义是什么,以及通过更改扩展名会产生什么差异。

Thanks in advance. 提前致谢。

EDIT 编辑

Here is the relevant code: 这是相关代码:

#pragma comment(lib,"ws2_32")

#include <WinSock2.h>
#include <stdio.h>


int main()
{
// Startup the winsock
WORD wVersionRequested;
WSADATA wsaData;
int wsaerr;
wVersionRequested = MAKEWORD(2,2);
wsaerr = WSAStartup(wVersionRequested,&wsaData);
if(wsaerr != 0)
{
    printf("Winsock2 dll is not found!\n");
    WSACleanup();
    return 0;
}
else
{
    printf("Winsock2 dll is found!\n");
    printf("Current System Status: %s.\n",wsaData.szSystemStatus);
}

//Create a SOCKET object called socketobj.
SOCKET socketobj;
socketobj = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketobj == INVALID_SOCKET)
{
    printf("Socket Intialization Failed with error: %ld\n", WSAGetLastError());
    WSACleanup();
    return 0;
}
else

{
    printf("Socket Intialization Success\n");
}

Sleep(10000);
return 0;
}

Without seeing the code it's hard to tell. 没有看到代码,很难说。

But my guess is that you have some interleaved declarations and code. 但我的猜测是你有一些交错的声明和代码。 MSVC's C compiler is only C89 which does not support it. MSVC的C编译器只是C89,它不支持它。 That would explain why the C++ compiler accepts it, but the C compiler doesn't. 这可以解释为什么C ++编译器接受它,但C编译器不接受它。

Prior to C99, all declarations must be at the start of the function or a block. 在C99之前,所有声明必须位于函数或块的开头。

EDIT : Your code doesn't show the whole function, but you probably have some (non-declaration) code before the SOCKET socketobj; 编辑:您的代码没有显示整个函数,但您可能在SOCKET socketobj;之前有一些(非声明)代码SOCKET socketobj; declaration. 宣言。


Now that the full function is shown, it confirms that you are interleaving declarations and code: 现在显示了完整的功能,它确认您正在交错声明和代码:

WORD wVersionRequested;            //  Declaration: ok
WSADATA wsaData;                   //  Declaration: ok
int wsaerr;                        //  Declaration: ok
wVersionRequested = MAKEWORD(2,2); //  Code: ok

...

SOCKET socketobj;                  //  Declaration: NOT ok
socketobj = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

The solution here is to move SOCKET socketobj; 这里的解决方案是移动SOCKET socketobj; to the start of the function with the other declarations. 与其他声明一起使用函数的开头。

There are differences between C and C++. C和C ++之间存在差异。 For example, in C89 you can't declare a variable in the middle of a block of code, but only at the beginning. 例如,在C89中,您不能在代码块的中间声明变量,而只能在开头声明。

Have a look at the error description: http://msdn.microsoft.com/en-us/library/76c9k4ah%28v=vs.71%29.aspx 看一下错误描述: http//msdn.microsoft.com/en-us/library/76c9k4ah%28v=vs.71%29.aspx

By changing the extension to .cpp Visual Studio uses the C++ compiler instead of the C compiler (which is quite a different language). 通过将扩展名更改为.cpp Visual Studio使用C ++编译器而不是C编译器(这是一种完全不同的语言)。

So you probably wrote C++ code and fed it into the C compiler, which (coincidently) resulted in that error beeing thrown. 所以你可能编写了C ++代码并将其输入到C编译器中,这恰好导致了错误的抛出。

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

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