Microsoft MVP성태의 닷넷 이야기
오류 유형: 544. C++ - fatal error C1017: invalid integer constant expression [링크 복사], [링크+제목 복사],
조회: 20926
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

C++ - fatal error C1017: invalid integer constant expression

C++의 #if 전처리 구문은,

#if, #elif, #else, and #endif Directives (C/C++)
; https://learn.microsoft.com/en-us/cpp/preprocessor/hash-if-hash-elif-hash-else-and-hash-endif-directives-c-cpp?view=vs-2019

해당 매크로가 정의된다면 반드시 정수 상수식임을 요구합니다. 만약 이를 만족시키지 못하면 C1017 컴파일 오류가 발생하는데요.

Fatal Error C1017
; https://learn.microsoft.com/en-us/cpp/error-messages/compiler-errors-1/fatal-error-c1017?view=vs-2019

문제는, 해당 매크로 상수가 정의되지 않았을 때에는 정수 상수식이 아니어도 된다는 점입니다.

#if _MYHEADER_H
short t1 = 5;
#else
#define _MYHEADER_H
short t1 = 5;
#endif

즉, 위의 상황에서는 "#if" 다음의 _MYHEADER_H 상수가 정의되지 않은 상태이므로 오류 없이 컴파일됩니다. 하지만, 일단 상수가 정의되었으면 그것은 반드시 정숫값을 가져야만 합니다. 위에서 "#define _MYHEADER_H"와 같이 하면 일단은 통과하는 듯 싶어도 다음과 같이 코드가 반복되면 여지없이 두 번째에서 C1017 오류가 발생합니다.

#if _MYHEADER_H
short t1 = 5;
#else
#define _MYHEADER_H
short t1 = 5;
#endif

#if _MYHEADER_H // fatal error C1017: invalid integer constant expression
short t1 = 5;
#else
#define _MYHEADER_H
short t1 = 5;
#endif

물론, 현업에서 저런 식으로 두 번 연이어 사용하는 경우는 거의 없을 것입니다. 단지 헤더 파일을 정의하는 과정에서 ("#pragma once" 처리를 실수로 누락한... 등의 사유로) #include가 다층으로 꼬이게 되면 저 오류가 어느 순간 돌연 발생하게 됩니다.

만약 위와 같은 상황을 C# 언어도 잘 사용하는 개발자에게 발생한다면... C#에서는 다음과 같은 처리가 정상적으로 처리되기 때문에,

#if _MYHEADER_H
#else
#define _MYHEADER_H
public class Test { }
#endif

#if _MYHEADER_H
public class Test2 { }
#else
#define _MYHEADER_H
#endif

이유를 찾지 못해 헤매는 경우가 나옵니다... 저처럼. ^^;

(첨부 파일은 C1017 오류가 재현되는 Visual C++ 예제입니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 3/9/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 106  107  108  109  110  111  112  113  114  115  116  117  [118]  119  120  ...
NoWriterDateCnt.TitleFile(s)
11035정성태8/29/201622523오류 유형: 354. .NET Reflector - PDB 생성 화면에서 "Clear Store"를 하면 "Index and length must refer to a location within the string" 예외 발생
11034정성태8/25/201626591개발 환경 구성: 296. .NET Core 프로젝트를 NuGet Gallery에 배포하는 방법 [2]
11033정성태8/24/201624651오류 유형: 353. coreclr 빌드 시 error C3249: illegal statement or sub-expression for 'constexpr' function
11032정성태8/23/201623835개발 환경 구성: 295. 최신의 Visual C++ 컴파일러 도구를 사용하는 방법 [1]
11031정성태8/23/201619683오류 유형: 352. Error encountered while pushing to the remote repository: Response status code does not indicate success: 403 (Forbidden).
11030정성태8/23/201623282VS.NET IDE: 111. Team Explorer - 추가한 Git Remote 저장소가 Branch에 보이지 않는 경우
11029정성태8/18/201630686.NET Framework: 602. Process.Start의 cmd.exe에서 stdin만 redirect 하는 방법 [1]파일 다운로드1
11028정성태8/15/201623114오류 유형: 351. Octave 설치 시 JRE 경로 문제
11027정성태8/15/201625283.NET Framework: 601. ElementHost 컨트롤의 메모리 누수 현상
11026정성태8/13/201626471Math: 19. 행렬 연산으로 본 해밍코드
11025정성태8/12/201625776개발 환경 구성: 294. .NET Core 프로젝트에서 "Copy to Output Directory" 처리 [1]
11024정성태8/12/201624485오류 유형: 350. "nProtect GameMon" 실행 중에는 Visual Studio 디버깅이 안됩니다! [1]
11023정성태8/10/201626015개발 환경 구성: 293. Azure 구독 후 PaaS 서비스 만들어 보기
11022정성태8/10/201626855개발 환경 구성: 292. Azure Cloud Service 배포시 사용자 정의 작업을 추가하는 방법
11021정성태8/10/201624012오류 유형: 349. System.Runtime.Remoting.RemotingException - Type '..., ..., Version=..., Culture=neutral, PublicKeyToken=null' is not registered for activation [2]
11020정성태8/10/201626827VC++: 98. 원본과 대상 버퍼가 같은 경우 memcpy, wmemcpy 주의점
11019정성태8/10/201643573기타: 60. 도서: 시작하세요! C# 6.0 프로그래밍: 기본 문법부터 실전 예제까지 (2쇄 정오표)
11018정성태8/9/201627550.NET Framework: 600. 단일 메서드 내에서의 할당으로 알아보는 자바와 닷넷의 GC 차이점 [1]
11017정성태8/9/201628309웹: 33. HTTP 쿠키에 한글 값을 설정하는 방법
11016정성태8/7/201626921개발 환경 구성: 291. Windows Server Containers 소개
11015정성태8/7/201625344오류 유형: 348. Windows Server 2016 TP5에서 Windows Containers의 docker run 실행 시 encountered an error during Start failed in Win32
11014정성태8/6/201625888오류 유형: 347. Hyper-V Virtual Machine Management service Account does not have permission to open attachment
11013정성태8/6/201636762개발 환경 구성: 290. Windows 10에서 경험해 보는 Windows Containers와 docker [4]
11012정성태8/6/201626943오류 유형: 346. Windows 10에서 Windows Containers의 docker run 실행 시 encountered an error during CreateContainer failed in Win32 발생
11011정성태8/6/201628153기타: 59. outlook.live.com 메일 서비스의 아웃룩 POP3 설정하는 방법
11010정성태8/6/201624918기타: 58. Outlook에 설정한 SMTP/POP3(예:천리안 메일) 계정 암호를 잊어버린 경우
... 106  107  108  109  110  111  112  113  114  115  116  117  [118]  119  120  ...