Thread: const modifier at end of signature

  1. #1
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13

    const modifier at end of signature

    Code:
    unsigned int Drib::nextBird(unsigned int visibility)//having a const here screws things up
    {
        return dielema.nextBird(visibility);
    }
    dielema is an instance of a class that I wrote. When I try to call this method above, if it still had the const, my compiler would give me an error thinking that I'm passing visibility by reference.

    I thought declaring a method as const just means that it's not going to change any of the private data members?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Is "nextBird" (and every function it calls) declared with "const" at the end also? Also, "nextBird" (and every function it calls) cannot modify the object (this)

  3. #3
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13
    no it's not declared const, but it is not a mutator method (nor is any other it calls)

    what reasoning is used to make it think that whenever I call the method declared as const that I'm passing it references?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Then declare it and every function it calls as const, or remove "const" from the single function thats causing problems above.

    Im not sure why it would complain about references, I havent done anything like this in a while. What is the exact error message?

    Also, if this is a recursive call, its infinite recursion, so you should fix that.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Marking a method as const means that it won't modify any member variables of that object. I'm assuming dielema is a member variable of the class? If that's the case, then you cannot call a non-const member variable of the dielema class because non-const functions are allowed to modify the state of the class.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Registered User help_seed's Avatar
    Join Date
    Nov 2009
    Posts
    13
    GoodGuy.cpp
    Code:
    unsigned int Drib::nextBird(unsigned int visibility)//having a const here screws things up
    {
        return dielema.nextBird(visibility);
    }
    GoodGuy.h
    [CODE]
    #ifndef LINKED_LIST_INCLUDED
    #define LINKED_LIST_INCLUDED
    #include "LinkedList.h"
    #endif

    const unsigned int SECONDS_WORKED = 60*60*10;//10 hour work day
    const unsigned int RATE = 8;//cages 8 birds in an hour
    const unsigned int MAX_BIRDS_CAGED = SECONDS_WORKED*RATE/60;
    const unsigned int TREE_HEIGHT = 20;//20 branches on the tree
    const unsigned int COLUMNS = 2;
    const unsigned int MIN_TIME = 7;
    const unsigned int MAX_TIME = 10;

    class Drib
    {
    public:
    Drib();
    unsigned int getSecondsWorked() const;
    unsigned int search(unsigned int maxVisibility, bool algorithm);
    void erase(unsigned int position);
    unsigned int nextBird(unsigned int visibility);//use to be const
    unsigned int getBranch(unsigned int bird);
    unsigned int getFirst() const;
    void remove(unsigned int location);

    private:
    unsigned int properTime;
    unsigned int rows;
    List dielema;
    unsigned int getRandomNumber(int nLow, int nHigh) const;
    double unifRand();

    };

    LinkedList.cpp
    Code:
    unsigned int List::nextBird(unsigned int t)
    {
        NodePtr temp = head;
        unsigned int current = 0;
        while(temp->next != NULL)
        {
            if((temp->time+BIRD_LAST_IN_CAGE) >= t)
                return current;
            else
            {
    
    /*the following 2 lines modify data members*/
    List::erase(current); birdsDied++; } current++; } }
    when I compile this I get:
    error: no matching function for call to `List::nextCagedBird(unsigned int&) const'
    note: candidates are: unsigned int List::nextCagedBird(unsigned int) <near match>

    so what's the deal with the &

    Drib.cpp:75: error: no matching function for call to `List::nextCagedBird(unsigned int&) const'
    LinkedList.cpp:201: note: candidates are: unsigned int List::nextCagedBird(unsigned int) <near match>

    another question, when I try to compile all I get is this:
    >C:\MinGW\bin\g++ -pedantic -Os main.cpp -o main.exe
    C:\DOCUME~1\Brendon\LOCALS~1\Temp/ccJ3tb2i.o:main.cpp.text+0x43e): undefined reference to `BadGuy::BadGuy()'
    collect2: ld returned 1 exit status
    >Exit code: 1

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by help_seed View Post
    I thought declaring a method as const just means that it's not going to change any of the private data members?
    No, it means that the method promises to not change any members, not just private ones but public and protected as well (excluding mutable).
    It wont allow you to make that promise if it sees that the function is calling a method which itself doesn't also make that promise.
    Last edited by iMalc; 12-06-2009 at 02:33 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    when I compile this I get:
    error: no matching function for call to `List::nextCagedBird(unsigned int&) const'
    note: candidates are: unsigned int List::nextCagedBird(unsigned int) <near match>

    so what's the deal with the &
    The & is completely irrelevant. Since there is no matching function, how is the compiler supposed to figure out whether that missing function takes the argument by reference or not?
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM