can anyone take a look at my program?

I have written code for merging two doubly linked list together. For some reason, it is not compiling.
Here is what I have so far :
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
#include<iostream>

using namespace std;

#define NULL 0

template <typename T>
class DoublyList;

template <typename T>
class Node
{
  friend class DoublyList<T>;
  public:
    Node()
      {next = this; prev = this;}
    Node(const T& data, Node<T> *next = NULL, Node<T> *prev)
    {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(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(someArray[i], arraySize);
  }
}

// This is a destructor

template<typename T>
DoublyList<T>::~DoublyList()
{
  while (header->next != header)
  {
    Node* 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(DoublyList<T> &listA)
{
  DoublyList<T> *result = NULL;    // build the answer here
  DoublyList<T> *current = *listA; //iterate over the original list
  DoublyList<T> *next;
  while(current != NULL)
  {
    next = current ->next;
    insert(&result, current);
    current = next;
  }
  *listA = result;
}

// 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 *next = header ->next;
    header = next;
    cout<<header;
  } 

}


Here is the main function
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();
}


This should print:

1 2 3 4 5 6 7
list is empty now


Thanks for any help provided! I know there gotta be a problem somewhere in my code!
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> ]

Thanks,
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.
It is still giving me 9 errors! I can't even comprehend what compilers are complaining about.
Look at the information starting from where it says "error" to the end of the line
I know how to look at errors. I meant to say I couldn't understand the error message!
Post the new errors (and the new source).
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
#include<iostream>

using namespace std;

#define NULL 0

template <typename T>
class DoublyList;

template <typename T>
class Node
{
  friend class 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?
Well, if it's actually supposed to do something but it doesn't, then I'd say there is a logic error :)

1
2
3
4
5
DoublyList<int> list1(array1, array1Size);
DoublyList<int> list2(array2, arraySize);
DoublyList<int> list3;
list1.mergeList(list2);
list3.print();


You merge list2 into list1, then print out the contents of the (empty) list3.
Topic archived. No new replies allowed.