错误 C2275:非法使用此类型作为表达式 [英] error C2275 : illegal use of this type as an expression

查看:33
本文介绍了错误 C2275:非法使用此类型作为表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我不知道从昨天起有什么变化,但是今天早上,我的代码不能再编译了.

I don't what has changed since yesterday, but this morning, my code can't compile anymore.

这是我的错误:

c:pathmain.c(56): error C2275: 'SERVICE_TABLE_ENTRY' : illegal use of this type as an expression
c:program filesmicrosoft sdkswindowsv7.0aincludewinsvc.h(773) : see declaration of 'SERVICE_TABLE_ENTRY'
c:pathmain.c(56): error C2146: syntax error : missing ';' before identifier 'DispatchTable'
c:pathmain.c(56): error C2065: 'DispatchTable' : undeclared identifier
c:pathmain.c(56): error C2059: syntax error : ']'
c:pathmain.c(57): error C2065: 'DispatchTable' : undeclared identifier
c:pathmain.c(57): warning C4047: 'function' : 'const SERVICE_TABLE_ENTRYA *' differs in levels of indirection from 'int'
c:pathmain.c(57): warning C4024: 'StartServiceCtrlDispatcherA' : different types for formal and actual parameter 1

以下是与这些错误相关的代码(从第 45 行到第 58 行):

Here's the code concerned by these errors (from lines 45 to 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;
}

这是我的 ServiceMain 函数的代码:

And here's the code of my ServiceMain function :

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 !

推荐答案

当您将源文件命名为 *.c 时,MSVC 假定它正在编译 C,也就是 C89.所有块局部变量都需要在块的开头声明.

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.

解决方法包括:

  • 在代码块的开头声明/初始化所有局部变量(直接在左大括号 { 之后)
  • 将源文件重命名为 *.cpp 或等效文件并编译为 C++.
  • 升级到 VS 2013,它放宽了此限制.
  • 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++.
  • upgrading to VS 2013, which relaxes this restriction.

这篇关于错误 C2275:非法使用此类型作为表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆