求助:Visual Studio 2017中Error C2259无法实例化抽象类问题
Hey there! I totally get how frustrating these compiler errors can be when you're just starting out with C++ classes and templates. Let's walk through exactly what's causing this Error C2259 in Visual Studio 2017 and how to fix it.
What's the Root Cause?
The error means you're trying to create an object of an abstract class. A class becomes abstract when it has one or more pure virtual functions (the ones marked with = 0). Your linkedListType class has exactly one pure virtual function:
virtual void inserFirst(const Type& newItem) = 0;
Abstract classes can't be instantiated directly—they're meant to be base classes that derived classes inherit from and fully implement all pure virtual functions.
Step-by-Step Fixes
1. Ensure Your Ordered List Class Uses Proper Public Inheritance
First, double-check that your ordered linked list class (let's assume it's named orderedLinkedListType) inherits publicly from linkedListType<Type>. Private/protected inheritance will hide the base class's pure virtual functions, leaving your derived class still abstract.
Example of correct inheritance:
template <class Type> class orderedLinkedListType : public linkedListType<Type> { // Your class members (like node pointers, helper functions) go here };
2. Implement the Pure Virtual Function inserFirst in Your Derived Class
You must provide a concrete implementation of inserFirst (note: double-check if this is a typo for insertFirst—make sure the function name matches exactly between base and derived classes!) in your ordered list class. Since it's an ordered list, your insertion logic should maintain the sorted order, not just add to the front blindly.
Here's a sample implementation (assuming you have a nodeType struct and base class members like first, last, and count):
template <class Type> void orderedLinkedListType<Type>::inserFirst(const Type& newItem) { nodeType<Type>* newNode = new nodeType<Type>; newNode->info = newItem; newNode->link = nullptr; if (this->isEmpty()) { this->first = newNode; this->last = newNode; } else { // Traverse to find the correct sorted position nodeType<Type>* current = this->first; nodeType<Type>* trailCurrent = nullptr; while (current != nullptr && current->info < newItem) { trailCurrent = current; current = current->link; } // Insert at the start of the list if (trailCurrent == nullptr) { newNode->link = this->first; this->first = newNode; } // Insert in the middle or end else { trailCurrent->link = newNode; newNode->link = current; if (current == nullptr) { this->last = newNode; } } } this->count++; }
3. Fix the Base Class Destructor
Your linkedListType has a non-virtual destructor. For polymorphic classes (base classes with virtual functions), always use a virtual destructor to ensure derived class destructors are called correctly and avoid memory leaks.
Update your base class destructor to be virtual with cleanup logic:
template <class Type> class linkedListType { public: virtual void inserFirst(const Type& newItem) = 0; virtual ~linkedListType() { // Clean up all nodes to prevent memory leaks nodeType<Type>* temp; while (first != nullptr) { temp = first; first = first->link; delete temp; } last = nullptr; count = 0; } // Don't forget to declare necessary members like first, last, count, isEmpty() };
Final Check
After making these changes, ensure you're not trying to create an object of linkedListType directly anywhere in your code. Instead, create objects of your derived orderedLinkedListType class—since it now implements all pure virtual functions, it's no longer abstract and can be instantiated.
内容的提问来源于stack exchange,提问作者Andriy Kukh

