J
Jeff0803
Guest
I moved codes from an algorithm book and trying to make it work.
Code is like following.
#include <iostream>
#include <vector>
#include <assert.h>
using namespace std;
const int defaultSize = 10;
// There are pure virtual functions in this class.
// Therefore this class is a abstract class
template <typename E>
class List { // List ADT
private:
void operator = (const List&) {}// Protect assignment
List(const List&) {}
public:
List() {} // Default constructor
virtual ~List() {} // Base destructor
// Clear contents from the list, to make it empty
virtual void clear() = 0;
// Insert an element at the current location
// item: The element to be inserted
virtual void insert(const E& item) = 0;
// Append an element at the current location.
// item: The element to be appended.
virtual void append(const E& item) = 0;
// Remove and return the current element
// Return: the element that was removed
virtual E remove() = 0;
// Set the current position to the start of the list
virtual void MoveToStart() = 0;
// Set the current position to the end of the list
virtual void MoveToEnd() = 0;
// Move the current position one step left. No change
// if already at beginning
virtual void prev() = 0;
// Move the current position one step right. No change
// if already at end
virtual void next() = 0;
// Return: The number of elements in the list.
virtual int length() const = 0; // A member function declared const( a const member function) cannot
// modify the value of a member of the object for which it is called
// Return: The position of the current element
virtual int currPos() const = 0;
// Set current position.
// pos: The position to make current.
virtual void moveToPos(int pos) = 0;
// Return: The current element.
virtual const E& getValue() const = 0;
};
template <typename E>
class AList : List<E> {
private:
int maxSize; // Protect assignment
int listSize; // Number of list items now
int curr; // Position of current element
E* listArray; // Array holding list elements
public:
AList(int size = defaultSize) { // Constructor
maxSize = size;
listSize = curr = 0;
listArray = new E[maxSize];
}
~AList() { delete[] listArray; }// Destructor
void clear() { // Reinitialize the list
delete[]listArray; // Remove the array
listSize = curr = 0; // Reset the size
listArray = new E[maxSize]; // Recreate array
}
// Insert "it" at current position
void insert(const E& it)
{
assert(listSize < maxSize, "List capacity exceeded");
for (int i = listSize; i > curr; i--) // Shift element up
listArray = listArray[i - 1]; // to make room
listArray[curr] = it;
listSize++; // Increment list size
}
void append(const E& it) {
assert(listSize < maxSize, "List capacity exceeded");
listArray[listSize++] = it;
}
// Remove and return the current element
E remove() {
assert((curr >= 0) && (curr < listSize), "No element");
E it = listArray[curr]; // Copy the element
for (int i = curr; i < listSize - 1; i++) // Shift them down
listArray = listArray[i + 1];
listSize--; // Decrement size
return it;
}
void moveToStart() { curr = 0; }
void MoveToEnd() { curr = listSize; }
void prev() { if (curr != 0) curr--; }
void next() { if (curr < listSize) curr++; }
// Return list size
int length() const { return listSize; }
// Return current position
int currPos() const { return curr; }
// Set current list position to "pos"
void moveToPos(int pos) {
assert((pos >= 0) && (curr < listSize), "Pos out of range");
return listArray[curr];
}
const E& getValue() const { // Return current element
assert((curr >= 0) && (curr < listSize), "No current element");
return listArray[curr];
}
};
Can anybody set initial int values(4, 9, 2, 8, 1, 5, 7, 3, 6, 10) to the AList and make the AList's member functions work?
I tried following code but error occurs.
AList<int> alist{4, 9, 2, 8, 1, 5, 7, 3, 6, 10 };
error C2259: 'AList<int>': cannot instantiate abstract class
I want to construct with initial values and make member functions work for example;
Add 11 to the list or remove 4'th value from the list.
Can anybody help me?
Continue reading...
Code is like following.
#include <iostream>
#include <vector>
#include <assert.h>
using namespace std;
const int defaultSize = 10;
// There are pure virtual functions in this class.
// Therefore this class is a abstract class
template <typename E>
class List { // List ADT
private:
void operator = (const List&) {}// Protect assignment
List(const List&) {}
public:
List() {} // Default constructor
virtual ~List() {} // Base destructor
// Clear contents from the list, to make it empty
virtual void clear() = 0;
// Insert an element at the current location
// item: The element to be inserted
virtual void insert(const E& item) = 0;
// Append an element at the current location.
// item: The element to be appended.
virtual void append(const E& item) = 0;
// Remove and return the current element
// Return: the element that was removed
virtual E remove() = 0;
// Set the current position to the start of the list
virtual void MoveToStart() = 0;
// Set the current position to the end of the list
virtual void MoveToEnd() = 0;
// Move the current position one step left. No change
// if already at beginning
virtual void prev() = 0;
// Move the current position one step right. No change
// if already at end
virtual void next() = 0;
// Return: The number of elements in the list.
virtual int length() const = 0; // A member function declared const( a const member function) cannot
// modify the value of a member of the object for which it is called
// Return: The position of the current element
virtual int currPos() const = 0;
// Set current position.
// pos: The position to make current.
virtual void moveToPos(int pos) = 0;
// Return: The current element.
virtual const E& getValue() const = 0;
};
template <typename E>
class AList : List<E> {
private:
int maxSize; // Protect assignment
int listSize; // Number of list items now
int curr; // Position of current element
E* listArray; // Array holding list elements
public:
AList(int size = defaultSize) { // Constructor
maxSize = size;
listSize = curr = 0;
listArray = new E[maxSize];
}
~AList() { delete[] listArray; }// Destructor
void clear() { // Reinitialize the list
delete[]listArray; // Remove the array
listSize = curr = 0; // Reset the size
listArray = new E[maxSize]; // Recreate array
}
// Insert "it" at current position
void insert(const E& it)
{
assert(listSize < maxSize, "List capacity exceeded");
for (int i = listSize; i > curr; i--) // Shift element up
listArray = listArray[i - 1]; // to make room
listArray[curr] = it;
listSize++; // Increment list size
}
void append(const E& it) {
assert(listSize < maxSize, "List capacity exceeded");
listArray[listSize++] = it;
}
// Remove and return the current element
E remove() {
assert((curr >= 0) && (curr < listSize), "No element");
E it = listArray[curr]; // Copy the element
for (int i = curr; i < listSize - 1; i++) // Shift them down
listArray = listArray[i + 1];
listSize--; // Decrement size
return it;
}
void moveToStart() { curr = 0; }
void MoveToEnd() { curr = listSize; }
void prev() { if (curr != 0) curr--; }
void next() { if (curr < listSize) curr++; }
// Return list size
int length() const { return listSize; }
// Return current position
int currPos() const { return curr; }
// Set current list position to "pos"
void moveToPos(int pos) {
assert((pos >= 0) && (curr < listSize), "Pos out of range");
return listArray[curr];
}
const E& getValue() const { // Return current element
assert((curr >= 0) && (curr < listSize), "No current element");
return listArray[curr];
}
};
Can anybody set initial int values(4, 9, 2, 8, 1, 5, 7, 3, 6, 10) to the AList and make the AList's member functions work?
I tried following code but error occurs.
AList<int> alist{4, 9, 2, 8, 1, 5, 7, 3, 6, 10 };
error C2259: 'AList<int>': cannot instantiate abstract class
I want to construct with initial values and make member functions work for example;
Add 11 to the list or remove 4'th value from the list.
Can anybody help me?
Continue reading...