C++ vtable Error — Complete Guide
이 글의 핵심
C++ vtable error C++, vtable, "undefined, Introduction: "undefined reference to vtable for MyClass" explained in detail with practical examples.
Introduction: “undefined reference to vtable for MyClass"
"Declared virtual function but got linker error”
In C++, declaring virtual function without defining it causes vtable linker error.
Below is an implementation example using C++. Define a class to encapsulate data and functionality. Try running the code directly to check its operation.
// ❌ vtable error
class Base {
public:
virtual void foo(); // Declaration only
virtual ~Base(); // Declaration only
};
// Link error:
// undefined reference to `vtable for Base'
What This Guide Covers:
- What is vtable?
- vtable error causes
- Solutions
- Pure virtual functions
1. What is vtable?
vtable (Virtual Table)
vtable is virtual function table that determines which function to call at runtime.
Here is detailed implementation code using C++. Define a class to encapsulate data and functionality. Understand the role of each part while examining the code.
class Base {
public:
virtual void foo() {
std::cout << "Base::foo\n";
}
virtual void bar() {
std::cout << "Base::bar\n";
}
};
class Derived : public Base {
public:
void foo() override {
std::cout << "Derived::foo\n";
}
};
// vtable structure (conceptual)
// Base vtable:
// [0] -> Base::foo
// [1] -> Base::bar
//
// Derived vtable:
// [0] -> Derived::foo
// [1] -> Base::bar
2. vtable Error Causes
Cause 1: Missing Virtual Function Definition
Below is an implementation example using C++. Define a class to encapsulate data and functionality. Try running the code directly to check its operation.
// ❌ No definition
// base.h
class Base {
public:
virtual void foo(); // Declaration only
virtual ~Base(); // Declaration only
};
// Link error: undefined reference to `vtable for Base'
Cause 2: Missing Destructor Definition
Below is an implementation example using C++. Define a class to encapsulate data and functionality. Understand the role of each part while examining the code.
// ❌ No destructor definition
class Base {
public:
virtual ~Base(); // Declaration only
virtual void foo() {
std::cout << "foo\n";
}
};
// Link error: undefined reference to `vtable for Base'
3. Solutions
Solution 1: Add Definition in .cpp
Here is detailed implementation code using C++. Define a class to encapsulate data and functionality. Understand the role of each part while examining the code.
// ✅ base.h
class Base {
public:
virtual void foo();
virtual ~Base();
};
// ✅ base.cpp
void Base::foo() {
std::cout << "Base::foo\n";
}
Base::~Base() {
std::cout << "~Base\n";
}
Solution 2: Inline Definition in Header
Below is an implementation example using C++. Define a class to encapsulate data and functionality. Understand the role of each part while examining the code.
// ✅ base.h
class Base {
public:
virtual void foo() {
std::cout << "Base::foo\n";
}
virtual ~Base() {
std::cout << "~Base\n";
}
};
Solution 3: Use = default
Below is an implementation example using C++. Define a class to encapsulate data and functionality. Try running the code directly to check its operation.
// ✅ base.h
class Base {
public:
virtual void foo() {
std::cout << "Base::foo\n";
}
virtual ~Base() = default; // Default implementation
};
4. Pure Virtual Functions
Pure Virtual Function with = 0
Here is detailed implementation code using C++. Define a class to encapsulate data and functionality. Understand the role of each part while examining the code.
// ✅ Pure virtual function
class Base {
public:
virtual void foo() = 0; // Pure virtual
virtual ~Base() = default;
};
class Derived : public Base {
public:
void foo() override {
std::cout << "Derived::foo\n";
}
};
// Base b; // Compile error: abstract class
Derived d; // OK
Summary
Key Points
- vtable: Virtual function table for runtime dispatch
- Error cause: Virtual function declared but not defined
- Solution: Provide definition in .cpp or inline
- Pure virtual: Use = 0 for abstract interface
- Destructor: Always define virtual destructor
When to Use
✅ Provide definition when:
- Declaring virtual functions
- Using virtual destructor
- Implementing polymorphic base class
❌ Don’t:
- Declare virtual function without definition
- Forget destructor definition
- Mix up pure virtual (= 0) with regular virtual
Best Practices
- ✅ Define all declared virtual functions
- ✅ Use = default for simple destructors
- ✅ Use = 0 for pure virtual functions
- ❌ Don’t declare virtual without definition
- ❌ Don’t forget virtual destructor definition
Related Articles
Master vtable for error-free C++ polymorphism! 🚀
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. Everything about C++ vtable Error : from basic concepts to practical applications. Master key content quickly with examp… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- C++ 가상 소멸자 | ‘메모리 누수’ 상속 클래스 소멸자 에러 해결
- C++ 가상 함수(Virtual Function)와 vtable의 동작 원리 [#33-1]
- C++ VTable | ‘가상 함수 테이블’ 가이드
이 글에서 다루는 키워드 (관련 검색어)
C++, vtable, linker-error, virtual, virtual-function, undefined-reference 등으로 검색하시면 이 글이 도움이 됩니다.