Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 16th, 2006, 3:46 AM   #1
Yona
Newbie
 
Join Date: Jan 2006
Posts: 2
Rep Power: 0 Yona is on a distinguished road
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!!!!
Yona is offline   Reply With Quote
Old Jan 16th, 2006, 4:37 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
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?
grumpy is offline   Reply With Quote
Old Jan 16th, 2006, 6:53 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
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;
to
const IntNode *current;
The Dark is offline   Reply With Quote
Old Feb 1st, 2006, 4:41 AM   #4
Yona
Newbie
 
Join Date: Jan 2006
Posts: 2
Rep Power: 0 Yona is on a distinguished road
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...
Yona is offline   Reply With Quote
Old Feb 1st, 2006, 4:58 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Yona
when i asked the tutor recently he said that in function list_from_multiplyWithNumber i should use the function list_multiplyWithNumber
What did he say?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC