Come on... the least you could do is post the compile errors, although even so I have to
wonder what is so hard about the compile errors that you can't even begin to fix them
yourself.
jsmith, I tried fixing the compiler errors with no success. I am getting weird compile errors.
List of compile errors :
1>c:\users\documents\visual studio 2008\projects\doublylist.h(17) : error C2548: 'Node<T>::Node' : missing default parameter for parameter 3
1> with
1> [
1> T=int
1> ]
1> c:\users\documents\visual studio 2008\projects\doublylist.h(51) : see reference to class template instantiation 'Node<T>' being compiled
1> with
1> [
1> T=int
1> ]
1> c:\users\documents\visual studio 2008\projects\doublylist.h(50) : while compiling class template member function 'DoublyList<T>::DoublyList(T [],T)'
1> with
1> [
1> T=int
1> ]
1> c:\users\documents\visual studio 2008\projects\doublylinkedlistdriver.cpp(10) : see reference to class template instantiation 'DoublyList<T>' being compiled
1> with
1> [
1> T=int
1> ]
1>c:\users\documents\visual studio 2008\projects\doublylist.h(55) : error C2664: 'DoublyList<T>::insert' : cannot convert parameter 1 from 'int' to 'Node<T> *'
1> with
1> [
1> T=int
1> ]
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\documents\visual studio 2008\projects\doublylist.h(64) : error C2440: 'initializing' : cannot convert from 'Node<T> *' to 'Node *'
1> with
1> [
1> T=int
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1> c:\users\documents\visual studio 2008\projects\doublylist.h(61) : while compiling class template member function 'DoublyList<T>::~DoublyList(void)'
1> with
1> [
1> T=int
1> ]
1>c:\users\documents\visual studio 2008\projects\doublylist.h(65) : error C2440: '=' : cannot convert from 'Node *' to 'Node<T> *'
1> with
1> [
1> T=int
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\documents\visual studio 2008\projects\doublylist.h(86) : error C2100: illegal indirection
1> c:\users\documents\visual studio 2008\projects\doublylist.h(84) : while compiling class template member function 'void DoublyList<T>::mergeList(DoublyList<T> &)'
1> with
1> [
1> T=int
1> ]
1>c:\users\documents\visual studio 2008\projects\doublylist.h(86) : error C2440: 'initializing' : cannot convert from 'DoublyList<T>' to 'DoublyList<T> *'
1> with
1> [
1> T=int
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\documents\visual studio 2008\projects\doublylist.h(90) : error C2039: 'next' : is not a member of 'DoublyList<T>'
1> with
1> [
1> T=int
1> ]
1>c:\users\documents\visual studio 2008\projects\doublylist.h(91) : error C2664: 'DoublyList<T>::insert' : cannot convert parameter 1 from 'DoublyList<T> **' to 'Node<T> *'
1> with
1> [
1> T=int
1> ]
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\documents\visual studio 2008\projects\doublylist.h(94) : error C2100: illegal indirection
1>c:\users\documents\visual studio 2008\projects\doublylist.h(94) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'DoublyList<T> *' (or there is no acceptable conversion)
1> with
1> [
1> T=int
1> ]
1> c:\users\documents\visual studio 2008\projects\doublydlist.h(39): could be 'DoublyList<T> &DoublyList<T>::operator =(const DoublyList<T> &)'
1> with
1> [
1> T=int
1> ]
1> while trying to match the argument list '(DoublyList<T>, DoublyList<T> *)'
1> with
1> [
1> T=int
1> ]
Take them one at a time. The first error is pretty explicit:
doublylist.h(17) : error C2548: 'Node<T>::Node' : missing default parameter for parameter 3 with [ T=int ]
Look at line 17. You are declaring a function which has 3 parameters. The second one you've provided a
default value for, but the third one you didn't. In C++, once one parameter is given a default value in the
declaration, all subsequent parameters must also be given default values.
Fix that one, recompile, and see what errors are left.
#include<iostream>
usingnamespace std;
#define NULL 0
template <typename T>
class DoublyList;
template <typename T>
class Node
{
friendclass DoublyList<T>;
public:
Node()
{next = this; prev = this;}
Node(const T& data, Node<T> *next = NULL, Node<T> *prev = NULL)
{this ->data = data; this ->next = next;
this ->prev = prev;}
private:
T data;
Node<T> *next;
Node<T> *prev;
};
template <typename T>
class DoublyList
{
public:
DoublyList();
DoublyList(T someArray[], T arraySize);
~DoublyList();
Node<T> *add(Node<T> *insert, const T& data);
void mergeList(const DoublyList<T>& listA);
void print();
private:
Node<T> *header;
int size; // number of data nodes in list
};
// Default constructor for creating empty linked list
template<typename T>
DoublyList<T>::DoublyList()
{
header = new Node<T>();
size = 0;
}
template<typename T>
DoublyList<T>::DoublyList(T someArray[], T arraySize)
{
header = new Node<T>();
size = arrSize;
for (int i = 0; i < size; i++)
{
add(header ->prev, someArray[i]);
}
}
// This is a destructor
template<typename T>
DoublyList<T>::~DoublyList()
{
while (header->next != header)
{
Node<T>* next = header ->next;
header = next;
}
delete header;
}
template<typename T>
Node<T> *DoublyList<T>::add(Node<T> *insert, const T& data)
{
Node<T> *prevPtr;
Node<T> *newPtr;
prevPtr = add ->prev;
newPtr = new Node<T>(data, add, prevPtr);
size++;
add ->prev = prevPtr->next = newPtr;
return newPtr;
}
// This function perform a sorted merge between the calling object list and the passed in listA
template<typename T>
void DoublyList<T>::mergeList(const DoublyList<T> &listA)
{
DoublyList<T> result; // build the answer here
DoublyList<T> current = listA; //iterate over the original list
while(header->next != header)
{
result = current ->next;
insert(&result, current);
current = next;
}
}
// This function print the values of each node in the list. If the list is empty it prints an error message
template<typename T>
void DoublyList<T>::print()
{
if(header->next == header)
{
cout<<"list is empty now"<<endl;
}
else
{
Node<T> *next = header ->next;
header = next;
cout<<header;
}
}
my .cpp file
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
int array1[] = {2,5,7};
int array2[] = {1,3,4,6};
int array1Size = sizeof(array1)/sizeof(int);
int array2Size = sizeof(array2)/sizeof(int);
DoublyList<int> list1(array1, array1Size);
DoublyList<int> list2(array2, arraySize);
DoublyList<int> list3;
list1.mergeList(list2);
list3.print();
}
Now it compiles but doesn't do anything! I wonder is there any logic error?