Results 1 to 4 of 4

Thread: weird problem when using class

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    weird problem when using class

    I made a histogram class and wanted to test it, but I'm getting errors like this:

    Code:
    303 C:\Dev-Cpp\Projects\Histography\main.cpp   instantiated from here
    which is this line:
    cout << hist.get_most_num_occurances();

    Code:
    101 C:\Dev-Cpp\Projects\Histography\main.cpp 'struct std::_Rb_tree_iterator<std::pair<const char, int> >' has no member named 'begin'
    Not sure.


    The main method looks like this:
    Code:
    int main()
    {
       map<char, int> my_map;
       my_map.insert( make_pair('a',2) );
       my_map.insert( make_pair('b',3) );
       my_map.insert( make_pair('c',1) );
       
       Histogram<char> hist(my_map);
       
       cout << hist.get_most_num_occurances();
       getchar();
    }
    The bold line is what gives the error.

    This is the function:

    Code:
    template <typename T> int Histogram<T>::get_most_num_occurances()
    {
             typename map<T,int>::iterator ci;
             int high = ci.begin();
             
             for (ci=frequency.begin(); ci != frequency.end(); ++ci)
             {
                 if (ci->second > high)
                 {
                    high = ci->second;
                 }
             }
    }

    Can anyone spot the problem? I've been beating my head on this one and can't figure it out.

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: weird problem when using class

    What is the type of frequency? Sounds like its type is 'std::_Rb_tree_iterator<std:air<const char, int> >' when youre using it like an object which has a member function named begin() which returns an iterator.

    It might be possible to just remove the ".begin()" from the line, but it's hard to say without knowing more about the code.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771

    Re: weird problem when using class

    Code:
    typename map<T,int>::iterator ci;
    int high = ci.begin();
    What are you doing here? You have an iterator 'ci', and you call its begin() method, iterators don't have a begin, you probably meant *frequency.begin(). (Note that this method will crash if the map is empty)

  4. #4

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: weird problem when using class

    Thanks guys. I made several VERY stupid mistakes, but the compiler didn't quit catch all of them. Without your help I would have been lost.

    As you guys pointed out, this line was incorrect:
    Code:
    typename map<T,int>::iterator ci;
    int high = ci.begin();
    I changed it to look like this:
    Code:
             typename map<T,int>::iterator ci = frequency.begin();
             
             int high = ci->second;
    That worked for me.


    Thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width