943,879 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 859
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 28th, 2009
0

Question on const Foo *const

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

cpp Syntax (Toggle Plain Text)
  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.
cpp Syntax (Toggle Plain Text)
  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

cpp Syntax (Toggle Plain Text)
  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. }

cpp Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
TheFueley is offline Offline
28 posts
since Mar 2008
Mar 28th, 2009
0

Re: Question on const Foo *const

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.
c++ Syntax (Toggle Plain Text)
  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;
Reputation Points: 38
Solved Threads: 13
Junior Poster
SeeTheLite is offline Offline
109 posts
since Mar 2009
Mar 28th, 2009
0

Re: Question on const Foo *const

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.

Click to Expand / Collapse  Quote originally posted by SeeTheLite ...
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.
c++ Syntax (Toggle Plain Text)
  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;
Reputation Points: 10
Solved Threads: 0
Light Poster
TheFueley is offline Offline
28 posts
since Mar 2008
Mar 28th, 2009
0

Re: Question on const Foo *const

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.
Reputation Points: 38
Solved Threads: 13
Junior Poster
SeeTheLite is offline Offline
109 posts
since Mar 2009
Mar 28th, 2009
0

Re: Question on const Foo *const

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
cpp Syntax (Toggle Plain Text)
  1. private: Node *previous, *next; string first, last;

Also, thank you for your help.
Reputation Points: 10
Solved Threads: 0
Light Poster
TheFueley is offline Offline
28 posts
since Mar 2008
Mar 28th, 2009
0

Re: Question on const Foo *const

Click to Expand / Collapse  Quote originally posted by TheFueley ...
"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.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 28th, 2009
0

Re: Question on const Foo *const

OH I see what you were doing, thought you were using a structure, not class lol, thought you made two single links >_<.
Reputation Points: 38
Solved Threads: 13
Junior Poster
SeeTheLite is offline Offline
109 posts
since Mar 2009
Mar 28th, 2009
0

Re: Question on const Foo *const

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...
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Mar 28th, 2009
0

Re: Question on const Foo *const

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.

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
You can use const_cast, i.e.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
TheFueley is offline Offline
28 posts
since Mar 2008
Mar 28th, 2009
0

Re: Question on const Foo *const

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.

Click to Expand / Collapse  Quote originally posted by ArkM ...
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...
Reputation Points: 10
Solved Threads: 0
Light Poster
TheFueley is offline Offline
28 posts
since Mar 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Visual C++ MFC issue with Splitter Windows
Next Thread in C++ Forum Timeline: Question about virtual functions





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


Follow us on Twitter


© 2011 DaniWeb® LLC