syntax stumped. simple.

This is a discussion on syntax stumped. simple. within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a bit of code that's giving me some trouble, but I can't make sense of the error. ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902

    syntax stumped. simple.

    Hello,
    I have a bit of code that's giving me some trouble, but I can't make sense of the error. It boils down to an ambiguous call that I can't, for the life of me, figure out why is ambiguous.

    Code then errors:
    Code:
    template<class numeric_type>
    struct diff_less_t
    {	
        diff_less_t(numeric_type subtracted) : sub(subtracted) {}
        bool operator() ( const typename std::map<numeric_type, numeric_type>::value_type & a,
    		      const typename std::map<numeric_type, numeric_type>::value_type & b )
        {
    	return std::abs(a.first-sub) < std::abs(b.first-sub);
        }
      private:
        numeric_type sub;
    };
    
    template<typename numeric_type>
    diff_less_t<numeric_type> diff_less(numeric_type sub) { return diff_less_t<numeric_type>(sub); }
    
    //. . . . quite a while later . . . 
    
    
    //xmatch is a long double
    map<long double, long double>::iterator it = min_element(points.begin(), points.end(), diff_less(xmatch));
    errors:
    Code:
    extractpoints.cpp: In member function ‘bool diff_less_t<numeric_type>::operator()(const typename std::map<numeric_type, numeric_type, std::less<_Key>, std::allocator<std::pair<const numeric_type, numeric_type> > >::value_type&, const typename std::map<numeric_type, numeric_type, std::less<_Key>, std::allocator<std::pair<const numeric_type, numeric_type> > >::value_type&) [with numeric_type = long double]’:
    /usr/include/c++/4.2/bits/stl_algo.h:4974:   instantiated from ‘_ForwardIterator std::min_element(_ForwardIterator, _ForwardIterator, _Compare) [with _ForwardIterator = std::_Rb_tree_iterator<std::pair<const long double, long double> >, _Compare = diff_less_t<long double>]’
    extractpoints.cpp:66:   instantiated from here
    extractpoints.cpp:16: error: call of overloaded ‘abs(long double)’ is ambiguous
    /usr/include/stdlib.h:691: note: candidates are: int abs(int)
    /usr/include/c++/4.2/cstdlib:143: note:                 long int std::abs(long int)
    /usr/include/c++/4.2/cstdlib:174: note:                 long long int __gnu_cxx::abs(long long int)
    extractpoints.cpp:16: error: call of overloaded ‘abs(long double)’ is ambiguous
    /usr/include/stdlib.h:691: note: candidates are: int abs(int)
    /usr/include/c++/4.2/cstdlib:143: note:                 long int std::abs(long int)
    /usr/include/c++/4.2/cstdlib:174: note:                 long long int __gnu_cxx::abs(long long int)
    Thanks for any help.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,165
    Include 'cmath' instead of 'math.h'.

    Soma

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    I hadn't included a math header at all

    Thanks.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 07-14-2009, 08:16 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. gcc asm code syntax
    By Uberapa in forum C Programming
    Replies: 4
    Last Post: 06-15-2007, 01:16 AM
  4. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  5. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21