Question on const Foo *const

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 26
Reputation: TheFueley is an unknown quantity at this point 
Solved Threads: 0
TheFueley's Avatar
TheFueley TheFueley is offline Offline
Light Poster

Question on const Foo *const

 
0
  #1
Mar 28th, 2009
Hello all,
I am trying to make a linked list (class-based). I'm not sure if I went actually did it right. I get a compiler error on the segment of code that deals with printing the linked list to stdout. Here is the seqment in question.

  1. // print contents of list
  2. void Node::printAll() const
  3. {
  4. Node *temp = NULL;
  5. temp = this;
  6. if (temp->previous != NULL)
  7. temp = findFirst(temp);
  8. else
  9. {
  10. do
  11. {
  12. // prints all items except last
  13. // since it tests for next to point to NULL
  14. cout << temp->first << " " << temp->last << endl;
  15. if (temp->next == NULL)
  16. continue;
  17. else
  18. temp = temp->next;
  19. } while (temp->next != NULL);
  20. }
  21. // print last item
  22. cout << temp->first << " " << temp->last << endl;
  23. }

The error my compiler displays is
"cannot convert from const Node *const to Node* Conversion loses qualifiers". I'm not sure how to fix this.

And here is the whole thing for reference.
  1. #ifndef NODE_H
  2. #define NODE_H
  3. #include <string>
  4. using std::string;
  5.  
  6. class Node
  7. {
  8. public:
  9. Node();
  10. Node* addToFront(Node*);
  11. Node* addToBack(Node*);
  12. Node* findFirst(Node*) const;
  13. Node* findLast(Node*) const;
  14. void printAll() const;
  15. private:
  16. Node *previous, *next;
  17. string first, last;
  18. void collectData(Node*);
  19. };
  20. #endif

  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::endl;
  5. #include <string>
  6. using std::string;
  7.  
  8. #include "Node.h"
  9.  
  10. Node::Node() : previous(NULL), next(NULL)
  11. {
  12. collectData(this);
  13. }
  14.  
  15. // add new Node to beginning
  16. Node* Node::addToFront(Node *n)
  17. {
  18. Node *temp = new Node;
  19. n = findFirst(n);
  20. temp->next = n;
  21. n->previous = temp;
  22. return temp;
  23. }
  24.  
  25. // add new Node to end
  26. Node* Node::addToBack(Node *n)
  27. {
  28. Node *temp = new Node;
  29. n = findLast(n);
  30. //point new object to what was last object
  31. temp->previous = n;
  32. // point what was last object to new last object
  33. n->next = temp;
  34. return temp;
  35. }
  36.  
  37. // returns first item in list
  38. Node* Node::findFirst(Node *n) const
  39. {
  40. // find 1st item in list
  41. if (n->previous != NULL)
  42. {
  43. while (n->previous != NULL)
  44. // point to previous object
  45. n = n->previous;
  46. }
  47. return n;
  48. }
  49.  
  50. // returns last item in list
  51. Node* Node::findLast(Node *n) const
  52. {
  53. // find last item
  54. if (n->next != NULL)
  55. {
  56. while (n->next != NULL)
  57. // point to next Node object
  58. n = n->next;
  59. }
  60. return n;
  61. }
  62.  
  63. // print contents of list
  64. void Node::printAll() const
  65. {
  66. Node *temp = NULL;
  67. temp = this;
  68. if (temp->previous != NULL)
  69. temp = findFirst(temp);
  70. else
  71. {
  72. do
  73. {
  74. // prints all items except last
  75. // since it tests for next to point to NULL
  76. cout << temp->first << " " << temp->last << endl;
  77. if (temp->next == NULL)
  78. continue;
  79. else
  80. temp = temp->next;
  81. } while (temp->next != NULL);
  82. }
  83. // print last item
  84. cout << temp->first << " " << temp->last << endl;
  85. }
  86.  
  87. // helper function; adds data to object
  88. void Node::collectData(Node *n)
  89. {
  90. string first, last;
  91. cout << "Enter first name:" << endl;
  92. cin >> first;
  93. n->first = first;
  94. cout << "Enter last name:" << endl;
  95. cin >> last;
  96. n->last = last;
  97. }

  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. using std::cin;
  5. #include "Node.h"
  6.  
  7. int main()
  8. {
  9. Node *me = new Node();
  10. me->addToBack(me);
  11. me->printAll();
  12. me->printAll();
  13. return 0;
  14. }
Rivera
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 109
Reputation: SeeTheLite is an unknown quantity at this point 
Solved Threads: 12
SeeTheLite SeeTheLite is offline Offline
Junior Poster

Re: Question on const Foo *const

 
0
  #2
Mar 28th, 2009
Your function is a bit, no way too complicated >_>, just keep one pointer to track the first link of your list and reference it when you want to print it.
  1.  
  2. Node *list, *front, *temp;
  3. list = new struct node;
  4. front = list
  5.  
  6. //your code here, do not modify "front"
  7.  
  8.  
  9. list = front; //print from begining
  10. while(list != NULL)
  11. cout<<list->data;
  12. list = list->next;
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: TheFueley is an unknown quantity at this point 
Solved Threads: 0
TheFueley's Avatar
TheFueley TheFueley is offline Offline
Light Poster

Re: Question on const Foo *const

 
0
  #3
Mar 28th, 2009
I'm a little confused on your reply.
You have a pointer-to-Node to track the beginning of the list but the beginning can change throughout the code. What was the first object may not always be the first object. That's why I have it find the first object and start from there. Then on lines 10-12 of your code you show a while-loop to print all the objects, but it wont print the last object as it is typed. On the last Node, list->next will point to NULL so the while condition test fails and moves on without printing the last object.

What my question really was about is how do I convert a const Node *const to Node*? The this pointer is type const Node *const. I apologize if I wasn't clear from the start.

Originally Posted by SeeTheLite View Post
Your function is a bit, no way too complicated >_>, just keep one pointer to track the first link of your list and reference it when you want to print it.
  1.  
  2. Node *list, *front, *temp;
  3. list = new struct node;
  4. front = list
  5.  
  6. //your code here, do not modify "front"
  7.  
  8.  
  9. list = front; //print from begining
  10. while(list != NULL)
  11. cout<<list->data;
  12. list = list->next;
Rivera
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 109
Reputation: SeeTheLite is an unknown quantity at this point 
Solved Threads: 12
SeeTheLite SeeTheLite is offline Offline
Junior Poster

Re: Question on const Foo *const

 
0
  #4
Mar 28th, 2009
As far as i can tell, this only a single link list, besides you can just redefine front everytime there is a new first link, it saves you alot of effort later on, and you will print EVERY element in the list because it isn't list->next, it is list that has to equal NULL. Lastly, a plain void function works just fine. Unless you are required to use a const, I would suggest you refrain from doing so, no need to make it harder than it actually is
Last edited by SeeTheLite; Mar 28th, 2009 at 1:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: TheFueley is an unknown quantity at this point 
Solved Threads: 0
TheFueley's Avatar
TheFueley TheFueley is offline Offline
Light Poster

Re: Question on const Foo *const

 
0
  #5
Mar 28th, 2009
Hmm. I thought having two pointers, one to the previous node and one to the next node qualified this as a doubly-linked list. I could be wrong.

From the header file
  1. private: Node *previous, *next; string first, last;

Also, thank you for your help.
Rivera
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Question on const Foo *const

 
0
  #6
Mar 28th, 2009
Originally Posted by TheFueley View Post
"cannot convert from const Node *const to Node* Conversion loses qualifiers". I'm not sure how to fix this.
You can use const_cast, i.e.
  1. Node * temp = const_cast<Node*>(this);
Maybe you could revise the 'constness' of the code (I did not look too closely). Perhaps read about Const correctness.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 109
Reputation: SeeTheLite is an unknown quantity at this point 
Solved Threads: 12
SeeTheLite SeeTheLite is offline Offline
Junior Poster

Re: Question on const Foo *const

 
0
  #7
Mar 28th, 2009
OH I see what you were doing, thought you were using a structure, not class lol, thought you made two single links >_<.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Question on const Foo *const

 
0
  #8
Mar 28th, 2009
Declare const Node* temp in const member functions - that's all. These functions must not modify Node fields but you can modify them via Node* temp pointer.

Yet another remark:
Did you want to implement Linked List class? If so why did you implement Node only class? A list is not a simple Node. Now you are trying to manipulate with list via Node pointers only in C (not C++ ) style. It's an example of a bad class design...
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: TheFueley is an unknown quantity at this point 
Solved Threads: 0
TheFueley's Avatar
TheFueley TheFueley is offline Offline
Light Poster

Re: Question on const Foo *const

 
0
  #9
Mar 28th, 2009
Yes, I've been reading that FAQ site. I got the idea from that site that if you create a function that *should not* modify the members it deals with you can/should declare it const. I also read that the const_cast is not preferred or dangerous? I'm just trying to learn the proper way to do this and since this is not a school project, I'm being a little picky about it. I appreciate your help. I will probably give up and make is simpler in the end anyway.

Originally Posted by mitrmkar View Post
You can use const_cast, i.e.
  1. Node * temp = const_cast<Node*>(this);
Maybe you could revise the 'constness' of the code (I did not look too closely). Perhaps read about Const correctness.
Rivera
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 26
Reputation: TheFueley is an unknown quantity at this point 
Solved Threads: 0
TheFueley's Avatar
TheFueley TheFueley is offline Offline
Light Poster

Re: Question on const Foo *const

 
0
  #10
Mar 28th, 2009
Interesting. I was mistakenly under the impression that a Linked list is just collection of nodes. Could you show me how I should be going about this? If you don't mind. I keep googling "linked list" and I only find examples using structs. Then I decided to go about it in a class, just to practice and whatnot. Thanks for your help.

Originally Posted by ArkM View Post
Declare const Node* temp in const member functions - that's all. These functions must not modify Node fields but you can modify them via Node* temp pointer.

Yet another remark:
Did you want to implement Linked List class? If so why did you implement Node only class? A list is not a simple Node. Now you are trying to manipulate with list via Node pointers only in C (not C++ ) style. It's an example of a bad class design...
Rivera
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC