Thread: Passing, AND modifying object values (passing const... discards qualifiers error)

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Passing, AND modifying object values (passing const... discards qualifiers error)

    So I am trying to pass an object to a function and access its methods, some of which modify its values. However, I haven't done this before, and I was following this example

    Use & to get object address : object pointer « Class « C++ Tutorial

    but since I keep getting

    passing `...' as `this' argument of `...' discards qualifiers

    I realize I am not supposed to be using "const", since that Object is not remaining constant. (vs, the example where they dont actually change the values)

    What should a function be, which is receiving an object in order to be modified

    If any examples could be provided or a link to another tutorial, that would help me out A LOT, thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What exactly is the code that you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Code:
    const Target *  const animateTarget( const Target *  const tar){
    
    ...
    tar->setValue(something)
    ....
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, the code example in the tutorial that you linked to compiles for me without even a warning. So, you should post the smallest and simplest example that demonstrates the problem, not a fragment that could be correct, or could be wrong, depending on what you don't show.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quite obviously you shouldn't insist that the object is const if you want to modify it (by calling non-const methods).

    Code:
    const Target *  const animateTarget( Target *  const tar){
        ...
        tar->setValue(something)
        ....
    }
    If the intention was really not to modify it, then you shouldn't be calling setValue() (or that method should be marked "const", which is hard to believe it is).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    @laserlight: I don't believe for one second you didn't immediately know what the problem was.
    Actually, I usually refuse to answer badly formulated questions as well, but I consider this one to be okay. I mean, even from his first post you can easily tell he tried to call a non-const function on a const object.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pointing out what the problem is as opposed to where the problem is sometimes just isn't helpful.
    Pointing out where and why is even more helpful.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed