Angband.oook.cz
Angband.oook.cz
AboutDownloadVariantsLadderForumCompetitionSpoilersComicScreenshotsFunniesLinks

Go Back   Angband Forums > Angband > Development

Reply
 
Thread Tools Display Modes
Old August 3, 2009, 09:11   #1
will_asher
DaJAngband Maintainer
 
will_asher's Avatar
 
Join Date: Apr 2007
Location: San Antonio, TX
Posts: 927
Donated: $10
will_asher is on a distinguished road
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

Play DaJAngband:
http://sites.google.com/site/dajangbandwebsite/home

Last edited by will_asher; August 7, 2009 at 00:26.
will_asher is offline   Reply With Quote
Old August 3, 2009, 10:20   #2
nekrotyrael
Rookie
 
Join Date: Jun 2009
Posts: 13
nekrotyrael is on a distinguished road
Const declares constants, which are, unlike variables, not modifiable once initialized.
nekrotyrael is offline   Reply With Quote
Old August 3, 2009, 16:43   #3
Pete Mack
Veteran
 
Join Date: Apr 2007
Location: Seattle, WA
Posts: 2,399
Donated: $40
Pete Mack is on a distinguished road
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)
Pete Mack is offline   Reply With Quote
Old August 3, 2009, 19:23   #4
zaimoni
Knight
 
zaimoni's Avatar
 
Join Date: Apr 2007
Posts: 590
zaimoni is on a distinguished road
Quote:
Originally Posted by will_asher View Post
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);
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.
Yes.

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:
Originally Posted by will_asher View Post
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?
In normal code such as Angband, const is used to convert runtime bugs into compiler errors, by declaring that "this code cannot change this object via this variable".

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
zaimoni is offline   Reply With Quote
Old August 3, 2009, 23:35   #5
will_asher
DaJAngband Maintainer
 
will_asher's Avatar
 
Join Date: Apr 2007
Location: San Antonio, TX
Posts: 927
Donated: $10
will_asher is on a distinguished road
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

Play DaJAngband:
http://sites.google.com/site/dajangbandwebsite/home
will_asher is offline   Reply With Quote
Reply


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

Similar Threads
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


All times are GMT +1. The time now is 07:41.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.