no appropriate default constructor available?

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.

Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifndef LINKED_LIST_H
#define LINKED_LIST_H

// File name: LinkedList.h
#include <iostream>

using namespace 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"
using namespace 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;
}


What should I do?
What should I do?

Show us the declaration for your Rectangle class.

If the compiler is complaining about a default constructor for your class, it would help to see the declaration for the class.

Last edited on
sorry i forgot it. here it is.
this header file is included in the other files(above it is not)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef BINARY_SEARCH_TREE_H
#define BINARY_SEARCH_TREE_H

#include "dsexceptions.h"
#include "LinkedList.h"
#include <iostream>    // For NULL
using namespace 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;

	friend class BinarySearchTree;
};




class BinarySearchTree
{... continues


it is too long i couldnt put the rest
Last edited on
When an object of class ListNode is being created the default consttructor of template type T is called

1
2
3
4
5
6
7
8
9
10
   struct ListNode
    {
      T value;
      ListNode * next;
      ListNode(T value1, ListNode * next1 = NULL)
      {
         value = value1;
         next = next1;
      }
    };

However class Rectangke that is used as the template argument has no the default constructor. So the error is issued.
Last edited on
Thanks a lot, it works when I add default constructor.
Topic archived. No new replies allowed.