STL problem

Im introducing in the STL programmnig, but i have some problems while compiling it. Look at this small part of code, and the compile error. Maybe you can help me with this error.

1
2
3
4
5
6
7
8
9
10
struct to_relative{
	pair<string, float> operator ()(pair<const string, float> &p){
		p.second=p.second/1000;
		return p;
		}
	};


void log_apache::AFrecuenciaRelativas(map<string,float> &m){
     transform (m.begin(),m.end(),m.begin(),to_relative());



And got the following error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
In file included from /usr/include/c++/4.4/bits/stl_algobase.h:66,
                 from /usr/include/c++/4.4/bits/char_traits.h:41,
                 from /usr/include/c++/4.4/ios:41,
                 from /usr/include/c++/4.4/istream:40,
                 from /usr/include/c++/4.4/fstream:40,
                 from log_apache.cpp:1:
/usr/include/c++/4.4/bits/stl_pair.h: In member function ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float>& std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float>::operator=(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float>&)’:
/usr/include/c++/4.4/bits/stl_pair.h:68:   instantiated from ‘_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float> >, _OIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float> >, _UnaryOperation = to_relative]’
log_apache.cpp:100:   instantiated from here
/usr/include/c++/4.4/bits/stl_pair.h:68: error: non-static const member ‘const std::basic_string<char, std::char_traits<char>, std::allocator<char> > std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float>::first’, can't use default assignment operator
In file included from /usr/include/c++/4.4/algorithm:62,
                 from log_apache.cpp:3:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float> >, _OIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float> >, _UnaryOperation = to_relative]’:
/usr/include/c++/4.4/bits/stl_algo.h:4703: note: synthesized method ‘std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float>& std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float>::operator=(const std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float>&)’ first required here 
 
Last edited on
Not quite sure,

but you are provding m as both your input and output vector. I think that is going to cause trouble later on.

Second you have const string, and the output message "can't use default assignment operator" makes me wonder, if maybe, the transform function does try to copy the elements into the new vector with operator= failing at it since you have a const string.
I was also going to suggest that const string maybe a problem.
I tried to compile it without the "const string", but i have another problem

1
2
3
4
5
6
7
In file included from /usr/include/c++/4.4/algorithm:62,
                 from log_apache.cpp:3:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float> >, _OIter = std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float> >, _UnaryOperation = to_relative]’:
log_apache.cpp:100:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:4703: error: no match for call to ‘(to_relative) (std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float>&)’
log_apache.cpp:57: note: candidates are: std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float> to_relative::operator()(std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float>&)


it looks like it is looking for a function with the const...

try calling it like
 
transform (m.begin(),m.end(),m.begin(),to_relative);


note no () after to_relative
Topic archived. No new replies allowed.