Results 1 to 4 of 4

Thread: std::containers within const functions

  1. #1
    Join Date
    Jun 2010
    Location
    Draper, UT
    Beans
    116
    Distro
    Ubuntu 11.04 Natty Narwhal

    std::containers within const functions

    This is rather frustrating... are std::list's and std::vector's begin() and end() not marked as const? Whenever I try to use them, I get the error:

    Code:
    Terrain.cpp: In member function ‘const Chunk* Terrain::getContainingChunk(float, float, float) const’:
    Terrain.cpp:40: error: conversion from ‘std::_List_const_iterator<Chunk*>’ to non-scalar type ‘std::_List_iterator<Chunk*>’ requested
    Which is annoying. I cant iterate through a std::list and at the same time call the member function const, even though it's not changing anything by iterating.

    There must be a better way to this. What might it be? My function must be const, and it must iterate through a std::list, returning the matching chunk of terrain.

  2. #2
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: std::containers within const functions

    Can you declare a const iterator and go thru this?

    Code:
        std::vector<YOUR_CLASS*>::const_iterator i;
        for(i = YOUR_LIST.begin(); i != YOUR_LIST.end(); ++i)
        {      
             i->YOUR_METHOD
        }
    ?

  3. #3
    Join Date
    Jun 2010
    Location
    Draper, UT
    Beans
    116
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: std::containers within const functions

    Would you look at that! Worked like a charm. I didn't know there was a separate const_iterator type.

    Thanks!

  4. #4
    Join Date
    Jun 2007
    Location
    Malvern, UK
    Beans
    992
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: std::containers within const functions

    No worries dude
    I wasn't familiar with it until a nice guy from this forum told me about it.

    mute

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •