Programing

오류 C2275 :이 유형을 표현식으로 잘못 사용했습니다.

lottogame 2021. 1. 10. 16:45
반응형

오류 C2275 :이 유형을 표현식으로 잘못 사용했습니다.


어제부터 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);
}

내 문제에 맞는 답을 찾을 수 없었습니다. 누구든지 도울 수 있습니까? 감사 !


소스 파일의 이름을 지정하면 *.cMSVC는 C89를 의미하는 C를 컴파일한다고 가정합니다. 모든 블록 로컬 변수는 블록의 시작 부분에 선언되어야합니다.

해결 방법은 다음과 같습니다.

  • 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.

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.


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).
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.


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.

ReferenceURL : https://stackoverflow.com/questions/9903582/error-c2275-illegal-use-of-this-type-as-an-expression

반응형