help with compile error (and "const")

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • will_asher
    DaJAngband Maintainer
    • Apr 2007
    • 1124

    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..)
    Last edited by will_asher; August 7, 2009, 00:26.
    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...)
  • nekrotyrael
    Rookie
    • Jun 2009
    • 16

    #2
    Const declares constants, which are, unlike variables, not modifiable once initialized.

    Comment

    • Pete Mack
      Prophet
      • Apr 2007
      • 6883

      #3
      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)

      Comment

      • zaimoni
        Knight
        • Apr 2007
        • 590

        #4
        Originally posted by will_asher
        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.

        Originally posted by will_asher
        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

        Comment

        • will_asher
          DaJAngband Maintainer
          • Apr 2007
          • 1124

          #5
          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...)

          Comment

          Working...
          😀
          😂
          🥰
          😘
          🤢
          😎
          😞
          😡
          👍
          👎