how to specify overloaded function

Discussion in 'C++' started by Amadeus W.M., Jul 29, 2009.

  1. Amadeus W.M.

    Amadeus W.M. Guest

    I wan to do this:

    #include <algorithm>

    //
    std::transform(Xj.begin(), Xj.end(), logXj.begin(), std::log);
    //


    where Xj and logXj are of type

    class Foo
    {
    public:
    typedef double * iterator;
    typedef const double * const_iterator;

    //

    };

    The compiler understandably gets confused by std::log, with the following
    error:


    fooMain.C:131: error: no matching function for call to ‘transform(double*,
    double*, double*, <unresolved overloaded function type>)’
    make: *** [main] Error 1


    How do I specify that I want

    double log(double) ?

    Thanks!
     
    Amadeus W.M., Jul 29, 2009
    #1
    1. Advertising

  2. On Jul 29, 6:45 am, "Amadeus W.M." <> wrote:
    > I wan to do this:
    >
    > #include <algorithm>
    >
    > //
    > std::transform(Xj.begin(), Xj.end(), logXj.begin(), std::log);
    > //
    >
    > where Xj and logXj are of type
    >
    > class Foo
    > {
    > public:
    >         typedef double * iterator;
    >         typedef const double * const_iterator;
    >
    > //
    >
    > };
    >
    > The compiler understandably gets confused by std::log, with the following
    > error:
    >
    > fooMain.C:131: error: no matching function for call to ‘transform(double*,
    > double*, double*, <unresolved overloaded function type>)’
    > make: *** [main] Error 1
    >
    > How do I specify that I want
    >
    > double log(double) ?
    >
    > Thanks!


    Just add a cast to the overloaded type you desire:

    #include <algorithm>
    #include <vector>
    #include <cmath>
    #include <iostream>
    using namespace std;
    int main()
    {
    vector<double> x;
    x.push_back(10);
    std::transform(x.begin(),x.end(),x.begin(),(double (*)(double))
    std::log);
    cout << *x.begin();
    }

    // Prints: 2.30259
     
    Mohammad Nabil Al-Aggan, Jul 29, 2009
    #2
    1. Advertising

  3. Amadeus W.M.

    Ian Collins Guest

    Amadeus W.M. wrote:
    > I wan to do this:
    >
    > #include <algorithm>
    >
    > //
    > std::transform(Xj.begin(), Xj.end(), logXj.begin(), std::log);
    > //
    >
    > How do I specify that I want
    >
    > double log(double) ?
    >

    The problem is probably cased by std::log having the wrong linkage
    (extern "C" for use in the C world).

    Either use a wrapper, or a cast.

    --
    Ian Collins
     
    Ian Collins, Jul 29, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nitin Bhardwaj
    Replies:
    8
    Views:
    1,058
    Andrey Tarasevich
    Jul 14, 2003
  2. Tobias Langner
    Replies:
    2
    Views:
    314
    Jakob Bieling
    Jul 31, 2003
  3. Roy Yao
    Replies:
    4
    Views:
    407
    Roy Yao
    Aug 21, 2003
  4. glen stark
    Replies:
    3
    Views:
    542
    glen stark
    Sep 30, 2003
  5. David Chandler
    Replies:
    3
    Views:
    515
    David Harmon
    Feb 2, 2004
Loading...

Share This Page