覆写虚函数时出现“unresolved external symbol”问题求助
ICopyOnOperator Interface Implementation Hey there, let's break down why you're hitting that unresolved external symbol error and how to fix it. The issue boils down to how you've implemented (or haven't implemented) the overridden operator= in your Vao class. Here's what you need to do:
1. Fully Implement the operator= in Vao
The base class ICopyOnOperator declares a pure virtual operator=, which means any concrete subclass like Vao must provide a complete implementation of this function. If you only declare the function in Vao without writing the function body, the linker will fail to find the required definition.
Here's how to properly implement it:
// Vao.h class Vao : public ICopyOnOperator<Vao> { // Your member variables go here (e.g., unsigned int m_vaoID; etc.) public: // Override and fully implement the assignment operator const Vao& operator=(const Vao& other) override { // Always add a self-assignment check first to avoid unnecessary work if (this != &other) { // Copy your member variables here // Example: // m_vaoID = other.m_vaoID; // m_buffer = other.m_buffer; // ... copy all other class members } return *this; } // Optional: If you need a custom destructor, declare/implement it here too // virtual ~Vao() = default; };
2. Verify Function Signature Match
Double-check that your Vao's operator= exactly matches the signature from the base class:
- Return type:
const T&(which becomesconst Vao&for your subclass) - Parameter:
const T&(which becomesconst Vao&) - Use the
overridekeyword (C++11+) to catch accidental signature mismatches at compile time—this will throw an error if you didn't actually override the base class function, a common gotcha that leads to this exact error.
If the signatures don't match, the compiler won't recognize your function as an override, leaving the base class's pure virtual function unimplemented, which will also trigger the unresolved external symbol error.
3. Ensure Proper Compilation & Linking
- If you put the
operator=implementation in a separate.cppfile (e.g.,Vao.cpp), make sure that file is included in your project's build targets. Missing source files during compilation/linking is a frequent cause of this error. - If you implement the function inside the class definition (like the example above), you don't have to worry about this, as the compiler will inline or include the definition properly.
Quick Note on Your Base Class Copy Constructor
Your ICopyOnOperator copy constructor uses this->operator=((const T&)other);—this is safe for your Vao class since other is always a Vao instance (because Vao inherits from ICopyOnOperator<Vao>). Just keep in mind that if you ever create another subclass of ICopyOnOperator, you'll want to ensure this cast is valid for that type too.
内容的提问来源于stack exchange,提问作者Moritz Seppelt

