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

时间:2022-01-10 18:53:28

Since yesterday, I've been facing a compiling error for my C project. The project itself consists on creating a service that will make some tasks.

从昨天开始,我一直面临着C项目的编译错误。项目本身包括创建一个可以完成某些任务的服务。

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 !

我无法找到适合我的问题的答案,有人能帮忙吗?谢谢!

5 个解决方案

#1


122  

When you name your source files *.c, MSVC assumes it's compiling C, which means C89. All block-local variables need to be declared at the beginning of the block.

当您命名您的源文件*。c, MSVC假设它在编译c,这意味着C89。所有块本地变量都需要在块开始时声明。

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,放松了这一限制。

#2


5  

You might be using a version of C that doesn't allow variables to be declared in the middle of a block. C used to require that variables be declared at the top of a block, after the opening { and before executable statements.

您可能使用的是C版本,它不允许在块中间声明变量。C用于要求在一个块的顶部声明变量,在打开{和before可执行语句之前。

#3


2  

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);
}

#4


2  

This error occurred when transferring a project from one installation to another (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.
In my small program, errors popped up regarding the placement in code of certain types e.g. HWND and HRESULT as well as on the different format of for loops , and C++ constructs like LPCTSTR, size_t, StringCbPrintf and BOOL. Comparison.
Changing the "Compile as" from Default to Compile as C++ Code (/TP) resolved it.

当将一个项目从一个安装转移到另一个安装时发生了这个错误(VS2015 => VS2010)。C代码实际上是在原始机器上被编译成c++的,在目标机器上的“默认”设置在项目属性\C/ c++ \高级\编译中,尽管源文件属于type *.cpp,但它仍然指向C。在我的小程序中,错误出现在某些类型的代码中,例如HWND和HRESULT,以及for循环的不同格式,以及c++结构,如LPCTSTR、size_t、StringCbPrintf和BOOL。比较。将“编译为”从默认改为编译为c++代码(/TP)解决了它。

#5


0  

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. When bug hunting, best to test all of your assumptions.

您可能想知道代码的要点。通过显式地转换到我认为的类型,当编译器抛出一个错误时,我意识到在试图将“this”发送给另一个对象的构造函数时,我忽略了类名前面的一些匈牙利符号。当bug搜索时,最好测试所有的假设。

#1


122  

When you name your source files *.c, MSVC assumes it's compiling C, which means C89. All block-local variables need to be declared at the beginning of the block.

当您命名您的源文件*。c, MSVC假设它在编译c,这意味着C89。所有块本地变量都需要在块开始时声明。

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,放松了这一限制。

#2


5  

You might be using a version of C that doesn't allow variables to be declared in the middle of a block. C used to require that variables be declared at the top of a block, after the opening { and before executable statements.

您可能使用的是C版本,它不允许在块中间声明变量。C用于要求在一个块的顶部声明变量,在打开{和before可执行语句之前。

#3


2  

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);
}

#4


2  

This error occurred when transferring a project from one installation to another (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.
In my small program, errors popped up regarding the placement in code of certain types e.g. HWND and HRESULT as well as on the different format of for loops , and C++ constructs like LPCTSTR, size_t, StringCbPrintf and BOOL. Comparison.
Changing the "Compile as" from Default to Compile as C++ Code (/TP) resolved it.

当将一个项目从一个安装转移到另一个安装时发生了这个错误(VS2015 => VS2010)。C代码实际上是在原始机器上被编译成c++的,在目标机器上的“默认”设置在项目属性\C/ c++ \高级\编译中,尽管源文件属于type *.cpp,但它仍然指向C。在我的小程序中,错误出现在某些类型的代码中,例如HWND和HRESULT,以及for循环的不同格式,以及c++结构,如LPCTSTR、size_t、StringCbPrintf和BOOL。比较。将“编译为”从默认改为编译为c++代码(/TP)解决了它。

#5


0  

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. When bug hunting, best to test all of your assumptions.

您可能想知道代码的要点。通过显式地转换到我认为的类型,当编译器抛出一个错误时,我意识到在试图将“this”发送给另一个对象的构造函数时,我忽略了类名前面的一些匈牙利符号。当bug搜索时,最好测试所有的假设。