![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 2
Rep Power: 0
![]() |
invalid conversion from `const IntNode*' to `IntNode*'
hi,
i am working on this assignment. i have a header IntNode given and based on this header i have to make a new header InrNodeEx in which i have to make the three following functions : -inline int list_sum(const IntNode *head) Returns sumary of list -inline void list_multilpy_withNumber(IntNode *head, int number) Multiplies all elements of list with a number -inline IntNode* list_from_multiplyWithNumber(const IntNode* source, int number) Multiplies all elements of list with a number without changing the source list IntNodeEx i've written is the following #include "IntNode.h"
//Returns sumary of list
inline int list_sum(const IntNode *head){
IntNode *current;
int sum=0;
current=head; //ERROR APPEARS HERE//
while(current){
sum = sum + current->data;
current=current->getNext();
}
return sum;
}
//Multiplies all elements of list with a number
inline void list_multilpy_withNumber(IntNode *head, int number){
IntNode *current=head;
while(current){
current->data=(current->data)*number;
current=current->getNext();
}
}
//Multiplies all elements of list with a number without changing the source list
inline IntNode* list_from_multiplyWithNumber(const IntNode* source, int number){
IntNode *current=source; //ERROR APPEARS HERE//
IntNode *target=0;
while(current){
list_insert(&target, new IntNode((current->data)*number));
current=current->getNext();
}
return target;
}the problem is that when i call the functions list_sum(const IntNode *) and list_from_multiplyWithNumber(const IntNode*, int) the compiler shows the following mistakes In function `int list_sum(const IntNode*)':nvalid conversion from `const IntNode*' to `IntNode*' In function `IntNode* list_from_multiplyWithNumber(const IntNode*, int)':invalid conversion from `const IntNode*' to `IntNode*' which is absolutely normal to happen but i really can't figure out any way to deal with these errors. Can anybody suggest any solution? thanx for ur time!!!! |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,192
Rep Power: 5
![]() |
The reason for the error is that the argument to both functions is a const pointer, but you're attempting to copy it to a non-const pointer. Doing such a thing would provide a direct means of modifying a const object, and the compiler refuses.
The solution is to use a technique that does not (implicitly or explicitly) to remove the const'ness of the pointer. A poor solution (which, if the person marking your exercise is awake, would result in lower marks) would be to bludgeon the compiler into submission by using a typecast. Is getNext() a const method of IntNode? If it is, you will not need to use a typecast. If it isn't, then whoever gave you the assignment should be shot as the only solution would be to use a typecast. Are you allowed to use recursion? |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 810
Rep Power: 4
![]() |
As grumpy said, you are copying the const pointer to a non-const variable. Try changing your local pointers to const.
In list_sum, change IntNode *current; const IntNode *current; |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jan 2006
Posts: 2
Rep Power: 0
![]() |
hi again,
thanx for ur help so far. by the way when i asked the tutor recently he said that in function list_from_multiplyWithNumber i should use the function list_multiplyWithNumber, but i stil, l cant figure out how to use it... |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
![]()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|