-
Linked List
Hello all, I have almost completed a program that will take 8 numbers from a user and store it in a linked list. Once that is done It will ask for another entry and determine the location of this new entry and how many items are currently bigger than the new entry. I have (pretty much") 95% of it written out but am having problems with my header file (below). I just have to modify my main.cpp file to complete the program but am having problems in the header where I'm getting errors in the codeLL function. I cannot figure out why it keeps giving off this error. I thought I had written everything correctly. Please let me know if you have any suggestions or if you see something that Im just not seeing
Thanks in advance
--------------------Configuration: Linked List Main - Win32 Debug--------------------
Compiling...
Linked List Main.cpp
c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(29) : warning C4183: 'deleteLL': member function definition looks like a ctor, but name does not match enclosing class
c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(139) : see reference to class template instantiation 'LinkedList<DataType>' being compiled
c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(32) : error C2063: 'copyLL' : not a function
c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(139) : see reference to class template instantiation 'LinkedList<DataType>' being compiled
c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(32) : error C2040: 'copyLL' : 'class LinkedList<DataType>::Node *(class LinkedList<DataType>::Node *)' differs in levels of indirection from 'int'
c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(139) : see reference to class template instantiation 'LinkedList<DataType>' being compiled
c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(32) : fatal error C1903: unable to recover from previous error(s); stopping compilation
c:\documents and settings\steve\desktop\project 4 linked list\linkedlist.h(139) : see reference to class template instantiation 'LinkedList<DataType>' being compiled
Error executing cl.exe.
Linked List Main.obj - 3 error(s), 1 warning(s)
Code:
#ifndef LINKEDLIST
#define LINKEDLIST
#include<iostream.h>
template<typename DataType>
class LinkedList
{
private:
class Node
{
public: // Makes it accessible (public) by class Node
DataType data;
Node *next;
};
Node *first;
int mySize;
LinkedList::deleteLL()
{
Node *tempP=first, *disposeP;
while (tempP!=0)
{
disposeP=tempP;
tempP=tempP->next;
delete disposeP;
}
return;
}
Node* LinkedList::copyLL(Node* source)
{
if (source==0)
return 0;
Node* newH=new Node;
Node *tempP=newH;
newH->data=source->data;
source=source->next;
while (source!=0)
{
tempP->next=newNode;
tempP->data=source->data
tempP=tempP->next;
source=source->next;
}
temp->next=0;
return newH;
}
public:
LinkedList::LinkedList()
{
mySize=0;
first=0;
}
LinkedList::~LinkedList()
{
deleteLL();
}
LinkedList::LinkedList( const LinkedList &source)
{
mySize=source.first;
first=copyLL(source.first);
}
LinkedList& LinkedList::operator=(const LinkedList& s)
{
if (this!=&s)
{
deleteLL();
mySize=s.mySize;
first=copyLL(s.first)
}
return *this;
}
bool LinkedList::empty()
{
return (first==0);
}
void LinkedList::insert(DataType item, unsigned pos)
{
if (pos>mySize+1)
{
cout<<"Illegal position to insert:"<<pos<<endl;
return;
}
mySize++
Node* newNode=newNode;
newNode->data=item;
Node* prev=first;
for (int i=1, i<pos, i++)
prev=prev->next;
newNode->next=prev->next;
prev->next=newNode;
}
void LinkedList::delete (dataType item)
{
mySize--;
Node* tempP=first;
Node* prev=0;
while (tempP!=0 && tempP->data!=item)
{
prev=tempP;
tempP=tempP->next;
}
if (tempP!=0 && tempP->data==item)
{
prev->next=tempP->next;
delete tempP;
}
else
cout<<"Item to delete not found"<<endl;
return;
}
int LinkedList::locate(DataType item)
{
int position=0;
Node* ptr=first;
while(ptr->data<item &&ptr!=0)
{
position++;
ptr=ptr->next;
}
return position
}
void LinkedList::traverse()
{
Node *temp=first;
while (tempP!=0)
{
Process(tempP->data);
tempP=tempP->next;
}
return;
}
};
#endif
--------------------------------------------------------------------------------
Hi I've included a basic main which I am using to test the header file
Code:
#include "LinkedList.h"
#include<string>
using namespace std;
main()
{
}
-
the deleteLL doesn't have a return type.
Try removing the pharase LinkedList:: in one of the functions. The compiler may feel this is a definition without a declaration. If it works for one function, do it for all of them.
-
Linked List
Hi, I gave deleteLL a return type and tried removing LinkedList from 2 functions but that still did not work. I am still getting the same 3 errors. Im just confused as to why its telling me that copyLL is not a function when deleteLL is almost the same and seems to be working.
-
If you haven't tried compiling with the LinkedList:: phrase in as well as out, I'd do so. If you have done it both ways, try compiling after placing the node class external to the linked list class rather than nested in the linked list class.
If you haven't posted that you've figured it out or if someone hasn't already posted a successful fix by the time I get home, I'll copy and paste to my compiler to see if I can figure it out.
-
Remove all LinkedList::
Only need them when you implement the functions outside the class.
Line 19:
add void beforeb deleteLL()
line 97:
delete is a keyword, not a valid function name.. rename it
the typename is DataTyp not dataType
-
This compiles fine for me. Runtime errors still possible, though.
Code:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
template<typename DataType>
class LinkedList
{
private:
class Node
{
public: // Makes it accessible (public) by class Node
DataType data;
Node *next;
};
Node *first;
int mySize;
void deleteLL()
{
Node *tempP=first, *disposeP;
while (tempP!=0)
{
disposeP=tempP;
tempP=tempP->next;
delete disposeP;
}
}
Node* copyLL(Node* source)
{
if (source==0)
return 0;
Node* newH=new Node;
Node *tempP=newH;
newH->data=source->data;
source=source->next;
while (source!=0)
{
tempP->next=newNode;
tempP->data=source->data
tempP=tempP->next;
source=source->next;
}
temp->next=0;
return newH;
}
public:
LinkedList()
{
mySize=0;
first=0;
}
~LinkedList()
{
deleteLL();
}
};
#endif;
//driver program
#include <iostream>
#include "linkedList.h"
using namespace std;
int main(int argc, char* argv[])
{
LinkedList<int> myLinkedList;
return 0;
}