Reputation: 4621
I have 4 C++ files, 2 headers, and 2 .cc files. This is just a proof of concept but I can't seem to get it right.
My first header looks like this:
#ifndef INT_LIST_H
#define INT_LIST_H
class IntList
{
public:
//Adds item to the end of the list
virtual void pushBack(int item) = 0;
};
#endif
My second header uses the first and looks like this:
#ifndef ArrayIntList_H
#define ArrayIntList_H
#include "IntList.h"
class ArrayIntList : public IntList
{
private:
int* arrayList;
int* arrayLength;
public:
//Initializes the list with the given capacity and length 0
ArrayIntList(int capacity);
//Adds item to the end of the list
virtual void pushBack(int item) = 0;
};
#endif
my first .cc file fills in the methods of the previous class:
#include <iostream>
#include "ArrayIntList.h"
ArrayIntList::ArrayIntList(int capacity)
{
//make an array on the heap with size capacity
arrayList = new int[capacity];
//and length 0
arrayLength = 0;
}
void ArrayIntList::pushBack(int item)
{
arrayList[*arrayLength] = item;
}
And this is my main function:
#include <iostream>
#include "ArrayIntList.h"
int main(int argc, const char * argv[])
{
ArrayIntList s(5);
}
When I run this in Xcode I get an error that "Variable ArrayIntList is an abstract class" I don't understand how this can be since I defined it in my above .cc file. Any ideas?
Upvotes: 1
Views: 411
Reputation: 20076
An abstract base class cannot inherit from another Abstract base class, remove the
= 0;
from your equation in ArrayIntList:
virtual void pushBack(int item) = 0;
Upvotes: 0
Reputation: 1334
You've got ArrayIntList::pushBack(int item)
declared as a pure virtual function. That's what that = 0
does. Remove the = 0
from ArrayIntList.h.
Also: you're using an int pointer instead of an int to track your array length.
Upvotes: 3
Reputation: 1334
In class ArrayIntList you are declaring a pure-virtual "virtual void pushBack(int item) = 0;" which you have already declared in the abstract parent IntList. All you need to do is declare it as "virtual void pushBack(int item);".
Upvotes: 0
Reputation: 880
In your declaration of the ArrayIntList class, you need to remove the "= 0" from the method declaration. You probably also need to declare arrayLength to be an int rather than a pointer to an int. Finally, since you're allocating memory for the array in your constructor, you should declare a destructor to free the memory when the object is destroyed:
class ArrayIntList : public IntList
{
private:
int* arrayList;
int arrayLength;
public:
//Initializes the list with the given capacity and length 0
ArrayIntList(int capacity);
virtual ~ArrayIntList() { delete arrayList; }
//Adds item to the end of the list
virtual void pushBack(int item);
};
Of course, the best way to handle the array list would be to use a std::vector<int>
instead so you don't have to manually handle the memory allocation and deallocation
Upvotes: 2
Reputation: 10348
On class ArrayIntList use this
virtual void pushBack(int item);
instead of this
virtual void pushBack(int item) = 0;
The reason is that when you assign a 0 to a function declaration, you are saying it is "pure", or not implemented. But you are doing that (implementing it) below.
Upvotes: 4