![]() |
#1 |
DaJAngband Maintainer
Join Date: Apr 2007
Location: San Antonio, TX
Posts: 1,124
Donated: $10
![]() |
help with compile error (and "const")
I'm getting a compile error in Dev C++ which points me to this line which I added to item_tester_okay() in object1.c*
<code> /* squelched or hidden items should be as good as non-existant */ if (squelch_hide_item(o_ptr)) return (FALSE); </code> I noticed that the declaration of o_ptr in item_tester_okay() says: const object_type *o_ptr and the declaration of o_ptr in squelch_hide_item() just says: object_type *o_ptr and I'm pretty sure the compile error has something to do with this. I learned all I know of C from two sources: Beej's silly guide to C and the Angband code itself**. Beej's guide is a beginner's guide and doesn't mention "const", so what exactly does "const" do? The weird thing about this is that Visual C++ compiles it just fine and doesn't give me this error. Usually Visual C++ has been a lot more picky about correct code than Dev C++. (* FYI, my code is based on vanilla 3.0.9e) (**EDIT: and some people here on the forums. You've been a big help. ..didn't want to seem ungrateful..)
__________________
Will_Asher aka LibraryAdventurer My old variant DaJAngband: http://sites.google.com/site/dajangbandwebsite/home (defunct and so old it's forked from Angband 3.1.0 -I think- but it's probably playable...) Last edited by will_asher; August 7, 2009 at 00:26. |
![]() |
![]() |
![]() |
#2 |
Rookie
Join Date: Jun 2009
Posts: 16
![]() |
Const declares constants, which are, unlike variables, not modifiable once initialized.
|
![]() |
![]() |
![]() |
#3 |
Prophet
Join Date: Apr 2007
Location: Seattle, WA
Posts: 6,801
Donated: $40
![]() |
Just add the const to the declaration of squelch_hide_item; it doesn't change the item, only the item type. Alternatively you can cast it away:
const_cast<object_type*>(o_ptr) |
![]() |
![]() |
![]() |
#4 | ||
Knight
Join Date: Apr 2007
Posts: 590
![]() |
Quote:
Proper course of action: * check if squelch_hide_item() will compile with const object_type *o_ptr . If so, use that fix of the incoming type. * if squelch_hide_item() errors with the above change, then the fix is going to have to go the other way. "Const is a virus". This is not a situation where const_cast is defensible. Quote:
It has other uses, mostly when programming hardware drivers and similar. (In normal code, const static data whose address is not taken can be completely optimized away by the as-if rule.)
__________________
Zaiband: end the "I shouldn't have survived that" experience. V3.0.6 fork on Hg. Zaiband 3.0.10 ETA Mar. 7 2011 (Yes, schedule slipped. Latest testing indicates not enough assert() calls to allow release.) Z.C++: pre-alpha C/C++ compiler system (usable preprocessor). Also on Hg. Z.C++ 0.0.10 ETA December 31 2011 |
||
![]() |
![]() |
![]() |
#5 |
DaJAngband Maintainer
Join Date: Apr 2007
Location: San Antonio, TX
Posts: 1,124
Donated: $10
![]() |
Thanks for the help, I just changed
bool squelch_hide_item(object_type *o_ptr) to bool squelch_hide_item(const object_type *o_ptr) (both in squelch.c and in externs.h) and it worked fine.
__________________
Will_Asher aka LibraryAdventurer My old variant DaJAngband: http://sites.google.com/site/dajangbandwebsite/home (defunct and so old it's forked from Angband 3.1.0 -I think- but it's probably playable...) |
![]() |
![]() |
![]() |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
"repeat" Command just little bit "smarter" | ClaytonAguiar | Vanilla | 8 | June 17, 2009 15:01 |
I finally understand how diving quicker can be "safer" | Wraitheist | Vanilla | 13 | September 19, 2008 17:25 |
Quick Diving versus "Recommended Depths" | Wraitheist | Vanilla | 14 | May 4, 2008 08:12 |
[Norseband] Help needed with "silly" time messages | ISNorden | Variants | 2 | April 4, 2008 21:06 |
Roguelike idea from "Lost Magic" DS game - Mr. @ teaches typing! | ekolis | Idle chatter | 10 | October 19, 2007 03:25 |