Thread: Error C2166: l-value specifies const object

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    8

    Error C2166: l-value specifies const object

    Code:
     65 int BasicMonster::calculatePower() const
     
    66 {
    67 if (weight > stamina)
    68 power -= (weight - stamina) * 2;
    69 
    70 if (health <= 0)
    71 power = 0;
    72 else if (health > 100)
    73 power += (health - 100) * 3;
    74 else if (health < 10)
    75 power *= health / 10.0;
    76 
    77 return power;
    78 }

    Error 1 error C2166: l-value specifies const object 68
    Error 2 error C2166: l-value specifies const object 71
    Error 3 error C2166: l-value specifies const object 73
    Error 5 error C2166: l-value specifies const object 75


    Please, could you tell me what I have to write to not have errors?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "const" means "constant" which means "does not change". So since you have this function marked as const (on line 65), that means you cannot change any members of the class. (In other words, if this is supposed to change some member of the class, then it cannot be a const function.)

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    8
    Thank you!!!

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you need to change those members from inside const functions, you can mark them as mutable.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is dangerous. You should really think it through if it makes sense to do that. For most things, it simply doesn't make sense. The only exception I can think of is thread safety.
    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.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Elysia, I agree, but I was throwing it out there as another option that the language allows. I have never had a need to use mutable in any of my code, but I can see how it might be useful in applications like reference counting.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not disagreeing with your reply. Just putting in a note of caution that I felt was missing.
    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

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  2. Replies: 6
    Last Post: 04-04-2010, 11:48 AM
  3. Replies: 1
    Last Post: 04-03-2009, 08:52 AM
  4. const object error
    By noob2c in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2003, 07:44 AM