Mar 15, 2014 at 4:02pm Mar 15, 2014 at 4:02pm UTC
I am getting an error trying to convert from nodeType<Type> to nodeType<Type>* in my recursiveSort function and I do not know why this is happening. Can anyone help?
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
#ifndef H_linkedListIterator
#define H_linkedListIterator
#include <iostream>
template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};
template <class Type>
class linkedListIterator
{
public :
linkedListIterator();
linkedListIterator(nodeType<Type>*);
Type operator *();
linkedListIterator<Type> operator ++();
bool operator ==(const linkedListIterator<Type>&) const ;
bool operator !=(const linkedListIterator<Type>&) const ;
private :
nodeType<Type> *current;
};
template <class Type>
linkedListIterator<Type>::linkedListIterator()
{
current = NULL;
}
template <class Type>
linkedListIterator<Type>::linkedListIterator(nodeType<Type> *ptr)
{
current = ptr;
}
template <class Type>
Type linkedListIterator<Type>::operator *()
{
return current->info;
}
template <class Type>
linkedListIterator<Type> linkedListIterator<Type>::operator ++()
{
current = current->link;
return *this ;
}
template <class Type>
bool linkedListIterator<Type>::operator ==(const linkedListIterator<Type>& other) const
{
return (current == other.current);
}
template <class Type>
bool linkedListIterator<Type>::operator !=(const linkedListIterator<Type>& other) const
{
return (current != other.current);
}
#endif
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
#ifndef H_linkedListType
#define H_linkedListType
#include "linkedListIterator.h"
#include <iostream>
#include <cassert>
using namespace std;
template <class Type>
class linkedListType
{
public :
const linkedListType<Type>& operator =(const linkedListType<Type>&); //
void initializeList(); //
bool isEmptyList() const ; //
void print() const ; //
int length() const ; //
void destroyList(); //
Type front() const ; //
Type back() const ; //
virtual bool search(const Type&) const = 0;
virtual void insertFirst(const Type&) = 0;
virtual void insertLast(const Type&) = 0;
virtual void deleteNode(const Type&) = 0;
linkedListIterator<Type> begin(); //
linkedListIterator<Type> end(); //
linkedListType(); //
linkedListType(const linkedListType<Type>&); //
~linkedListType(); //
protected :
int count;
nodeType<Type> *first;
nodeType<Type> *last;
private :
void copyList(const linkedListType<Type>&); //
};
template <class Type>
bool linkedListType<Type>::isEmptyList() const
{
return (first == NULL);
}
template <class Type>
linkedListType<Type>::linkedListType()
{
first = NULL;
last = NULL;
count = 0;
}
template <class Type>
void linkedListType<Type>::destroyList()
{
nodeType<Type> *temp;
while (first != NULL)
{
temp = first;
first = first->link;
delete temp;
}
last = NULL;
count = 0;
}
template <class Type>
void linkedListType<Type>::initializeList()
{
destroyList();
}
template <class Type>
void linkedListType<Type>::print() const
{
nodeType<Type> *current;
current = first;
while (current != NULL)
{
cout << current->info << " " ;
current = current->link;
}
}
template <class Type>
int linkedListType<Type>::length() const
{
return count;
}
template <class Type>
Type linkedListType<Type>::front() const
{
assert(first != NULL);
return first->info;
}
template <class Type>
Type linkedListType<Type>::back() const
{
assert(last != NULL);
return last->info;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::begin()
{
linkedListIterator<Type> temp(first);
return temp;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::end()
{
linkedListIterator<Type> temp(NULL);
return temp;
}
template <class Type>
void linkedListType<Type>::copyList(const linkedListType<Type>& other)
{
nodeType<Type> *newNode, *current;
if (first != NULL)
destroyList();
if (other.first == NULL)
{
first = NULL;
last = NULL;
count = 0;
}
else
{
current = other.first;
count = other.count;
first = new nodeType<Type>;
first->info = current->info;
first->link = NULL;
last = first;
current = current->link;
while (current != NULL)
{
newNode = new nodeType<Type>;
newNode->info = current->info;
newNode->link = NULL;
last->link = newNode;
last = newNode;
current = current->link;
}
}
}
template <class Type>
linkedListType<Type>::~linkedListType()
{
destroyList();
}
template <class Type>
linkedListType<Type>::linkedListType(const linkedListType<Type>& other)
{
first = NULL;
copyList(other);
}
template <class Type>
const linkedListType<Type>& linkedListType<Type>::operator =(const linkedListType<Type>& other)
{
if (this != &other)
{
copyList(other);
}
return *this ;
}
#endif
Last edited on Mar 16, 2014 at 5:22pm Mar 16, 2014 at 5:22pm UTC
Mar 16, 2014 at 5:22pm Mar 16, 2014 at 5:22pm UTC
Would anyone mind compiling my code and letting me know what is happening? Any help is appreciated and thank you in advance.
Mar 16, 2014 at 5:51pm Mar 16, 2014 at 5:51pm UTC
You have
nodeType<Type> mergeList(nodeType<Type>*,nodeType<Type>*);
but in your recursiveSort function, you do
head = mergeList(head,otherHead);
So you try to assign a value of type nodeType<Type> to head , which is a nodeType<Type>* variable.
Mar 21, 2014 at 12:13am Mar 21, 2014 at 12:13am UTC
Can anyone help me fix this?
Mar 21, 2014 at 1:46am Mar 21, 2014 at 1:46am UTC
As @ long double main pointed out, your mergeList function needs to return a pointer and that is exactly what the error message is referring to.
Also remember to pass your pointers by ref if you wish to modify the underlying list
Mar 22, 2014 at 6:26am Mar 22, 2014 at 6:26am UTC
This was one of those errors that made me feel stupid. I had a meeting at work this morning and while waiting for it to begin, I started looking over my code and had a major epiphany and realized that I didn't have the return type of my mergeList function as a pointer. Problem solved! Thanks to all who replied.