hi,
I have a linked list code, it works with integers or strings, but when I try to implement it with a class that I write, I get " no appropriate default constructor available" error.
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
// File name: LinkedList.h
#include <iostream>
usingnamespace std;
// Use of templates.
template <class T>
class LinkedList
{
private:
struct ListNode
{
T value;
ListNode * next;
ListNode(T value1, ListNode * next1 = NULL)
{
value = value1;
next = next1;
}
};
ListNode * head; // List head pointer
public:
LinkedList() { head = NULL; } //Constructor
~LinkedList(); // Destructor
void add(T value);
void remove(T value);
void displayList();
};
//*********************************************
// Adds a new element to the end of the list. *
//*********************************************
template <class T>
void LinkedList<T>::add(T value)
{
if (head == NULL)
head = new ListNode(value);
else
{
// The list is not empty.
// Use nodePtr to traverse the list
ListNode * nodePtr = head;
while (nodePtr->next != NULL)
nodePtr = nodePtr->next;
// nodePtr->next is NULL so nodePtr points to the last node.
// Create a new node and put it after the last node.
nodePtr->next = new ListNode(value);
}
}
//**********************************************
// Removes a number from a list. The function *
// does not assume that the list is sorted. *
//**********************************************
template <class T>
void LinkedList<T>::remove(T value)
{
ListNode * nodePtr;
ListNode *previousNodePtr;
// If the list is empty, do nothing.
if (!head) return;
// Determine if the first node is the one to delete.
if (head->value == value)
{
nodePtr = head;
head = head->next;
delete nodePtr;
}
else
{
// Initialize nodePtr to the head of the list.
nodePtr = head;
// Skip nodes whose value member is not num.
while (nodePtr != NULL && nodePtr->value != value)
{
previousNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNodePtr->next = nodePtr->next;
delete nodePtr;
}
}
}
//************************************************
// displayList outputs a sequence of all values *
// currently stored in the list. *
//************************************************
template <class T>
void LinkedList<T>::displayList()
{
ListNode * nodePtr = head; // Start at head of list
while (nodePtr)
{
// Print the value in the current node
cout << nodePtr->value << endl;
// Move on to the next node
nodePtr = nodePtr->next;
}
}
//******************************************************
// Destructor deallocates the memory used by the list. *
//******************************************************
template <class T>
LinkedList<T>::~LinkedList()
{
ListNode * nodePtr = head; // Start at head of list
while (nodePtr != NULL)
{
// garbage keeps track of node to be deleted
ListNode * garbage = nodePtr;
// Move on to the next node, if any
nodePtr = nodePtr->next;
// Delete the "garbage" node
delete garbage;
}
}
#endif
and this is my main function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include "LinkedList.h"
usingnamespace std;
// Test program
int main( )
{
Rectangle extent(100,100,1,1);
LinkedList<Rectangle> list;
list.add(extent);
cout << endl <<"heyy" << endl;
int cc;
cin>>cc;
return 0;
}
#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H
#include "dsexceptions.h"
#include "LinkedList.h"
#include <iostream> // For NULL
usingnamespace std;
// BinarySearchTree class
//
// CONSTRUCTION: with ITEM_NOT_FOUND object used to signal failed finds
//
// ******************PUBLIC OPERATIONS*********************
// void insert( x ) --> Insert x
// void remove( x ) --> Remove x
// bool contains( x ) --> Return true if x is present
// Rectangle findMin( ) --> Return smallest item
// Rectangle findMax( ) --> Return largest item
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// void printTree( ) --> Print tree in sorted order
// ******************ERRORS********************************
// Throws UnderflowException as warranted
class Rectangle
{
public:
Rectangle(int tp, int lt, int bt, int rt)
{
Top = tp;
Left = lt;
Bottom = bt;
Right = rt;
CenterX = (lt + rt) / 2;
CenterY = (bt + tp) / 2;
}
void setRectangle(int tp, int lt, int bt, int rt)
{
Top = tp;
Left = lt;
Bottom = bt;
Right = rt;
CenterX = (lt + rt) / 2;
CenterY = (bt + tp) / 2;
}
void printRectangle()
{
cout << Top << " " << Left << " " << Bottom << " " << Right;
}
private:
int Top; // y coordinate of the upper edge
int Left; // x coordinate of the topLeft edge
int Bottom; // y coordinate of the bottom edge
int Right; // x coordinate of the topRight edge
int CenterX;
int CenterY;
friendclass BinarySearchTree;
};
class BinarySearchTree
{... continues