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.
Re: weird problem when using class
What is the type of frequency? Sounds like its type is 'std::_Rb_tree_iterator<std::pair<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.
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)
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.