gpt4 book ai didi

c - 错误 C2275 : illegal use of this type as an expression

转载 作者:太空狗 更新时间:2023-10-29 16:17:50 25 4
gpt4 key购买 nike

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

我不知道自昨天以来发生了什么变化,但今天早上,我的代码无法再编译。

这是我遇到的错误:

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

这是与这些错误有关的代码(从第 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;
}

这是我的 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);
}

我找不到适合我问题的答案,有人可以帮忙吗?谢谢!

最佳答案

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

解决方法包括:

  • 在代码块的开头声明/初始化所有局部变量(紧跟在大括号 { 之后)
  • 将源文件重命名为 *.cpp 或等效文件并编译为 C++。
  • 升级到 VS 2013,relaxes this restriction .

关于c - 错误 C2275 : illegal use of this type as an expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9903582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com