简体   繁体   English

错误C2275:非法使用此类型作为表达式

[英]error C2275 : illegal use of this type as an expression

Since yesterday, I've been facing a compiling error for my C project. 从昨天开始,我一直在为C项目遇到编译错误。 The project itself consists on creating a service that will make some tasks. 该项目本身包括创建将执行某些任务的服务。

I don't what has changed since yesterday, but this morning, my code can't compile anymore. 自昨天以来,我没有什么改变,但是今天早上,我的代码无法编译了。

Here are the errors I have : 这是我的错误:

c:\path\main.c(56): error C2275: 'SERVICE_TABLE_ENTRY' : illegal use of this type as an expression
c:\program files\microsoft sdks\windows\v7.0a\include\winsvc.h(773) : see declaration of 'SERVICE_TABLE_ENTRY'
c:\path\main.c(56): error C2146: syntax error : missing ';' before identifier 'DispatchTable'
c:\path\main.c(56): error C2065: 'DispatchTable' : undeclared identifier
c:\path\main.c(56): error C2059: syntax error : ']'
c:\path\main.c(57): error C2065: 'DispatchTable' : undeclared identifier
c:\path\main.c(57): warning C4047: 'function' : 'const SERVICE_TABLE_ENTRYA *' differs in levels of indirection from 'int'
c:\path\main.c(57): warning C4024: 'StartServiceCtrlDispatcherA' : different types for formal and actual parameter 1

Here's the code concerned by these errors (from lines 45 to 58) : 这是与这些错误有关的代码(从第45行到第58行):

int main(int ac, char *av[])
{
    if (ac > 1)
    {
        if (!parse_args(ac, av))
        {
        aff_error(ARGUMENTS);
        return EXIT_FAILURE;
        }
    }
    SERVICE_TABLE_ENTRY DispatchTable[] = {{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
    StartServiceCtrlDispatcher(DispatchTable);
    return EXIT_SUCCESS;
}

And here's the code of my ServiceMain function : 这是我的ServiceMain函数的代码:

void WINAPI ServiceMain(DWORD ac, LPTSTR *av)
{
    gl_ServiceStatus.dwServiceType = SERVICE_WIN32;
    gl_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
    gl_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
    gl_ServiceStatus.dwWin32ExitCode = 0;
    gl_ServiceStatus.dwServiceSpecificExitCode = 0;
    gl_ServiceStatus.dwCheckPoint = 0;
    gl_ServiceStatus.dwWaitHint = 0;
    gl_ServiceStatusHandle = RegisterServiceCtrlHandler(MY_SERVICE_NAME, ServiceCtrlHandler);
    if (gl_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
        return;
    gl_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
    gl_ServiceStatus.dwCheckPoint = 0;
    gl_ServiceStatus.dwWaitHint = 0;
    SetServiceStatus(gl_ServiceStatusHandle, &gl_ServiceStatus);
}

I couldn't manage to find some answers that fit my problem, could anyone helps ? 我无法找到适合我问题的答案,有人可以帮忙吗? Thanks ! 谢谢 !

When you name your source files *.c , MSVC assumes it's compiling C, which means C89. 当您将源文件命名为*.c ,MSVC假定它正在编译C,即C89。 All block-local variables need to be declared at the beginning of the block. 所有块局部变量都需要在块的开始处声明。

Workarounds include: 解决方法包括:

  • declaring/initializing all local variables at the beginning of a code block (directly after an opening brace { ) 在代码块的开头(直接在大括号{ )声明/初始化所有局部变量
  • rename the source files to *.cpp or equivalent and compile as C++. 将源文件重命名为*.cpp或等效文件,然后编译为C ++。
  • upgrading to VS 2013, which relaxes this restriction . 升级到VS 2013,从而放宽了此限制

You might be using a version of C that doesn't allow variables to be declared in the middle of a block. 您可能使用的C版本不允许在块的中间声明变量。 C used to require that variables be declared at the top of a block, after the opening { and before executable statements. C过去常要求变量在块{的开头,在{之后,在可执行语句之前)声明。

Put braces around the code where the variable is used. 在使用该变量的代码周围放括号。

In your case that means: 在您的情况下,这意味着:

if (ac > 1)
{
    if (!parse_args(ac, av))
    {
        aff_error(ARGUMENTS);
        return EXIT_FAILURE;
    }
}
{
    SERVICE_TABLE_ENTRY DispatchTable[] = {{MY_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
    StartServiceCtrlDispatcher(DispatchTable);
}

This error occurred when transferring a project from one installation to another (VS2015 => VS2010). 将项目从一种安装转移到另一种安装时发生此错误(VS2015 => VS2010)。
The C code was actually compiled as C++ on the original machine, on the target machine the "Default" setting in Project Properties\\C/C++\\Advanced\\Compile as was somehow pointing to C even though the source file was of type *.cpp. 实际上,C代码是在原始计算机上编译为C ++的,而在目标计算机上,即使源文件的类型为* .cpp,它在Project Properties\\C/C++\\Advanced\\Compile as的“默认”设置也以某种方式指向了C 。
In my small program, errors popped up regarding the placement in code of certain types eg HWND and HRESULT as well as on the different format of for loops , and C++ constructs like LPCTSTR, size_t , StringCbPrintf and BOOL . 在我的小程序中,关于某些类型的代码(例如HWNDHRESULT )以及不同格式的for循环以及C ++构造(例如LPCTSTR, size_tStringCbPrintfBOOL Comparison . 比较
Changing the "Compile as" from Default to Compile as C++ Code (/TP) resolved it. 将“编译为”从Default更改Compile as C++ Code (/TP)解决。

This will also give you "illegal use of this type as an expression". 这也将使您“非法使用此类型作为表达式”。

WRONG: 错误:

MyClass::MyClass()
{
   *MyClass _self = this;
}

CORRECT: 正确:

MyClass::MyClass()
{
   MyClass* _self = this;
}

You might be wonder the point of that code. 您可能想知道代码的重点。 By explicitly casting to the type I thought it was, when the compiler threw an error, I realized I was ignoring some hungarian notation in front of the class name when trying to send "this" to the constructor for another object. 通过显式转换为我认为是的类型,当编译器抛出错误时,我意识到当试图将“ this”发送给另一个对象的构造函数时,我忽略了类名前面的一些匈牙利符号。 When bug hunting, best to test all of your assumptions. 寻找错误时,最好测试一下所有假设。

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

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