Sam
Sam

Reputation: 193

cannot declare variable to be of abstract type

I have to make a double linked list class which inherits from a DoubleListInterface.h

I'm compiling as I go, and I've run into an error:

project3.cpp:19:21: error: cannot declare variable "list" to be of abstract type 'DoubleList<int>'
   DoubleList<int> list;
                   ^

From what I've read from another thread on here, "You need to define the function with the exact same argument types as in the base class."

I don't know why but that's just not clicking in my head.

project3.cpp:

#include <iostream>
#include <stdlib.h>
#include "DoubleList.cpp"

int failures = 0;

void test(int result, int expected) {
    if (result != expected) {
        std::cout << "test FAILED: expected(" << expected << ") but result(" << result << ")" << std::endl;
        failures++;
    }
}

int main(int argc, char **argv) {
    DoubleList<int> list;

    test(list.isEmpty(), true);
    test(list.getLength(), 0);

    list.insertFront(5);
    list.insertFront(7);
    list.insertBack(8);
    list.insertFront(2);

    test(list.isEmpty(), false);
    test(list.getLength(), 4);
    test(list.getEntry(1), 2);
    test(list.getEntry(2), 7);
    test(list.getEntry(3), 5);
    test(list.getEntry(4), 8);

    list.remove(1);
    test(list.getLength(), 3);
    test(list.getEntry(1), 7);
    test(list.getEntry(2), 5);
    test(list.getEntry(3), 8);

    list.remove(3);
    test(list.getLength(), 2);
    test(list.getEntry(1), 7);
    test(list.getEntry(2), 5);

    list.clear();
    test(list.isEmpty(), true);
    test(list.getLength(), 0);

    if (failures == 0)
        std::cout << "ALL TESTS PASSED" << std::endl;
    else
        std::cout << failures << " TESTS FAILED" << std::endl;

    return 0;
}

DoubleList.h:

#ifndef DOUBLE_LIST_
#define DOUBLE_LIST_

#include "DoubleListInterface.h"
#include "DoubleNode.cpp"

template<class ItemType>
class DoubleList : public DoubleListInterface<ItemType>
{
private:
    ItemType item;
    DoubleList<ItemType>* head;
    DoubleList<ItemType>* tail;

protected:
    // PUT DOUBLELIST METHODS HERE

public:
    // PUT INTERFACE METHODS HERE

    //DoubleList(const ItemType& anItem, DoubleList<ItemType>* headPtr, DoubleList<ItemType>* tailPtr);
    //~DoubleList(); <---------- Not sure if I need these or if I even implemented them right, so they're commented out for now

    bool isEmpty() const;
    int getLength() const;
    bool insertFront(const ItemType& newEntry);
    bool insertBack(const ItemType& newEntry);
    bool remove(int position);
    void clear();
    ItemType getEntry(int position);
};

#endif

DoubleList.cpp (so far):

#include "DoubleListInterface.h"
#include "DoubleList.h"
#include <iostream>

int itemCount = 0;

template<class ItemType>
bool DoubleList<ItemType>::isEmpty() const
{
    if (this->head == nullptr)
        return true;
    else
        return false;
}

template<class ItemType>
int DoubleList<ItemType>::getLength() const
{
    return 0;
}

template<class ItemType>
bool DoubleList<ItemType>::insertFront(const ItemType& newEntry)
{
    return true;
}

DoubleListInterface.h:

template<class ItemType>
class DoubleListInterface
{
public:
    /** Sees whether this list is empty.
      @return True if the list is empty; otherwise returns false. */
    virtual bool isEmpty() const = 0;

    /** Gets the current number of entries in this list.
      @return The integer number of entries currently in the list. */
    virtual int getLength() const = 0;

    /** Inserts an entry into this list at the front.
      @pre  None.
      @post  If the insertion is successful, newEntry is at the front of
        the list, other entries are renumbered accordingly, and the returned value
        is true.
      @param newEntry  The entry to insert into the list.
      @return  True if insertion is successful, or false if not. */
    virtual bool insertFront(const ItemType& newEntry) = 0;

     /** Inserts an entry into this list at the back.
        @pre  None.
        @post  If the insertion is successful, newEntry is at the back of
          the list, other entries are renumbered accordingly, and the returned value
          is true.
        @param newEntry  The entry to insert into the list.
        @return  True if insertion is successful, or false if not. */
     virtual bool insertBack(const ItemType& newEntry) = 0;

    /** Removes the entry at a given position from this list.
      @pre  None.
      @post  If 1 <= position <= getLength() and the removal is successful,
        the entry at the given position in the list is removed, other
        items are renumbered accordingly, and the returned value is true.
      @param position  The list position of the entry to remove.
      @return  True if removal is successful, or false if not. */
    virtual bool remove(int position) = 0;

    /** Removes all entries from this list.
      @post  List contains no entries and the count of items is 0. */
    virtual void clear() = 0;

    /** Gets the entry at the given position in this list.
      @pre  1 <= position <= getLength().
      @post  The desired entry has been returned.
      @param position  The list position of the desired entry.
      @return  The entry at the given position. */
    virtual ItemType getEntry(int position) const = 0;

    virtual ~DoubleListInterface() { }
};

#endif

Sorry if it's a lot, I'm just really lost right now.

Upvotes: 0

Views: 5409

Answers (1)

user7860670
user7860670

Reputation: 37600

ItemType getEntry(int position); does not override corresponding DoubleListInterface method. note the lack of const qualifier. You should use override specifier to avoid such problems

// Would trigger compilation error when base class
// does not have a virtual method with this exact signature.
ItemType getEntry(int position) override;

Upvotes: 2

Related Questions