943,070 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 23014
  • C++ RSS
Jul 21st, 2004
0

Linked List

Expand Post »
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)


C++ Syntax (Toggle Plain Text)
  1. #ifndef LINKEDLIST
  2. #define LINKEDLIST
  3. #include<iostream.h>
  4.  
  5. template<typename DataType>
  6. class LinkedList
  7. {
  8. private:
  9. class Node
  10. {
  11. public: // Makes it accessible (public) by class Node
  12. DataType data;
  13. Node *next;
  14. };
  15.  
  16. Node *first;
  17. int mySize;
  18.  
  19. LinkedList::deleteLL()
  20. {
  21. Node *tempP=first, *disposeP;
  22. while (tempP!=0)
  23. {
  24. disposeP=tempP;
  25. tempP=tempP->next;
  26. delete disposeP;
  27. }
  28. return;
  29. }
  30.  
  31. Node* LinkedList::copyLL(Node* source)
  32. {
  33. if (source==0)
  34. return 0;
  35. Node* newH=new Node;
  36. Node *tempP=newH;
  37. newH->data=source->data;
  38. source=source->next;
  39. while (source!=0)
  40. {
  41. tempP->next=newNode;
  42. tempP->data=source->data
  43. tempP=tempP->next;
  44. source=source->next;
  45. }
  46. temp->next=0;
  47. return newH;
  48. }
  49.  
  50. public:
  51. LinkedList::LinkedList()
  52. {
  53. mySize=0;
  54. first=0;
  55. }
  56. LinkedList::~LinkedList()
  57. {
  58. deleteLL();
  59. }
  60.  
  61. LinkedList::LinkedList( const LinkedList &source)
  62. {
  63. mySize=source.first;
  64. first=copyLL(source.first);
  65. }
  66.  
  67. LinkedList& LinkedList::operator=(const LinkedList& s)
  68. {
  69. if (this!=&s)
  70. {
  71. deleteLL();
  72. mySize=s.mySize;
  73. first=copyLL(s.first)
  74. }
  75. return *this;
  76. }
  77. bool LinkedList::empty()
  78. {
  79. return (first==0);
  80. }
  81. void LinkedList::insert(DataType item, unsigned pos)
  82. {
  83. if (pos>mySize+1)
  84. {
  85. cout<<"Illegal position to insert:"<<pos<<endl;
  86. return;
  87. }
  88. mySize++
  89. Node* newNode=newNode;
  90. newNode->data=item;
  91. Node* prev=first;
  92. for (int i=1, i<pos, i++)
  93. prev=prev->next;
  94. newNode->next=prev->next;
  95. prev->next=newNode;
  96. }
  97. void LinkedList::delete (dataType item)
  98. {
  99. mySize--;
  100. Node* tempP=first;
  101. Node* prev=0;
  102. while (tempP!=0 && tempP->data!=item)
  103. {
  104. prev=tempP;
  105. tempP=tempP->next;
  106. }
  107. if (tempP!=0 && tempP->data==item)
  108. {
  109. prev->next=tempP->next;
  110. delete tempP;
  111. }
  112. else
  113. cout<<"Item to delete not found"<<endl;
  114. return;
  115. }
  116.  
  117. int LinkedList::locate(DataType item)
  118. {
  119. int position=0;
  120. Node* ptr=first;
  121. while(ptr->data<item &&ptr!=0)
  122. {
  123. position++;
  124. ptr=ptr->next;
  125. }
  126. return position
  127. }
  128.  
  129. void LinkedList::traverse()
  130. {
  131. Node *temp=first;
  132. while (tempP!=0)
  133. {
  134. Process(tempP->data);
  135. tempP=tempP->next;
  136. }
  137. return;
  138. }
  139. };
  140. #endif



--------------------------------------------------------------------------------

Hi I've included a basic main which I am using to test the header file

C++ Syntax (Toggle Plain Text)
  1. #include "LinkedList.h"
  2. #include<string>
  3. using namespace std;
  4. main()
  5. {
  6.  
  7.  
  8.  
  9. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
silicon is offline Offline
15 posts
since Jul 2004
Jul 21st, 2004
0

Re: Linked List

LinkedList::deleteLL() has no return value. That's why the compiler thinks it looks like constructor.

Also, minor point, the LinkedList:: is not nescessary since you are enclosed in that class.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Jul 21st, 2004
0

Re: 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.

Quote originally posted by Chainsaw ...
LinkedList::deleteLL() has no return value. That's why the compiler thinks it looks like constructor.

Also, minor point, the LinkedList:: is not nescessary since you are enclosed in that class.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
silicon is offline Offline
15 posts
since Jul 2004
Jul 22nd, 2004
0

Re: Linked List

You changed it to something like "void deleteLL()" and you still get the complaint that "'deleteLL': member function definition looks like a ctor, but name does not match enclosing class"?

That's weird; maybe because its a template I'm not reading it right.

Another thing to think about is to pass a const ref to DataType when you pass it in for searching or copying. That way if it is of any significant size you won't be making extra work (making temp copies and using the copy constructor) for the compiler.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Builder 6
Next Thread in C++ Forum Timeline: File processing problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC