C++
VC++ Compile Error 모음
Elan
2023. 1. 25. 13:49
Pointer to incomplete class type is not allowed
위와 같이 cpp 파일에서 Session 타입에 대하여 incomplete class type 에러가 뜬다면
Session 헤더가 추가되어 있지 않아서 그런 것이 대부분이다.
맨위의 주석된 #include "Session.h"를 추가하면 해결 된다.
no default constructor exists for class
ClientService는 Service를 상속받은 class 이다.
그런데 ClientService에서 'no default constructor exists for class "Service" '라는 오류가 뜬다.
이유는 ClientService의 생성자에서 부모 class인 Service의 생성자 호출부분을 정의해주지 않아서 그렇다.
아래와 같이 부모 class인 Service의 생성자 호출 부분까지 정의해주면 해결된다.
error C4430 : missing type specifier - int assumed [duplicate]
error C2061 : syntax error : missing ';' before identifier 'XXXX'
error C2535 : 'XXXX': member function already defined or declared
주로 헤더 파일에서 순환 참조가 발생할 경우 컴파일 단계에서 무한 루프가 생기기 때문에 발생하거나,
헤더 파일을 include 해주지 않아서 발생한다.
unresolced external symbol "~"
헤더 파일에 static으로 선언한 멤버를 소스 파일에서 초기화 해주지 않은 경우 발생한다.
그러나 종종 초기화는 했으나 namespace를 명시 하지 않는 실수를 많이한다.(아래 예시 참고)
// 헤더 파일
class SocketUtils
{
public:
static LPFN_CONNECTEX ConnectEx;
static LPFN_DISCONNECTEX DisconnectEx;
static LPFN_ACCEPTEX AcceptEx;
static std::atomic<bool> IsInitialized;
public:
static void Init();
// ...
}
// 소스 파일
LPFN_CONNECTEX SocketUtils::ConnectEx = nullptr;
LPFN_DISCONNECTEX SocketUtils::DisconnectEx = nullptr;
LPFN_ACCEPTEX SocketUtils::AcceptEx = nullptr;
std::atomic<bool> IsInitialized = false; // 여기에 namespace를 SocketUtils::로 명시해주지 않음
// std::atomic<bool> SocketUtils::IsInitialized = false; // static 멤버는 namespace를 명시해주어야 함
std::atomic<bool> SocketUtils::IsInitialized = false;
void SocketUtils::Init()
{
bool expected = false;
if (IsInitialized.compare_exchange_strong(expected, true, std::memory_order_acquire))
return;
// ...
}
static 멤버 선언 후 초기화를 잊지말자.
※ 참고로 :: 연산자는 scope resolution operator라고 부른다.