friend template in namespace error

Discussion in 'C++' started by Pierre Yves, Jan 11, 2010.

  1. Pierre Yves

    Pierre Yves Guest

    Hello,

    I've got a little piece of code that is giving me grief. The example
    I've included demonstrates the problem (the code will make no sense i.e
    why have friend functions when I'm not accessing private members etc but
    the real code does actually have proper implementation). This code
    should go in a header file then just build.

    //==============================================================
    #ifndef _TEST_H_
    #define _TEST_H_

    #include <math.h>

    namespace sb
    {
    template<class T>
    class CVector;

    template<class S>
    CVector<S> pow(const CVector<S> & A, int n);

    template<class S>
    void SVD(const CVector<S> A);

    template <class T>
    class CVector
    {
    public:
    CVector();
    virtual ~CVector ();

    template<class S>
    friend CVector<S> pow(const CVector<S> & A, int n);

    template <class S>
    friend void SVD(const CVector<S> A);
    };

    template<class T>
    CVector<T> pow(const CVector<T> & A, int n)
    {
    CVector<T> S=A;
    for (unsigned int i=1; i<n; i++)
    {
    S = S*A;
    }
    return S;
    }

    template <class T>
    void SVD(const CVector<T> A)
    {
    double result = pow(2.0,6.0);
    }
    }

    #endif
    //==============================================================

    The error is:

    In file included from main.cpp:2:
    test.h: In function ‘void sb::SVD(sb::CVector<S>)’:
    test.h:44: error: no matching function for call to ‘pow(double, double)’

    It seems as if I can't use pow in math.h even though its included. Its
    like my definition of pow is the only one. My compiler is gcc 4.2.5.

    Anyone got any hints to what is wrong?

    Cheers,

    Pierre.
    Pierre Yves, Jan 11, 2010
    #1
    1. Advertising

  2. Pierre Yves wrote:
    > I've got a little piece of code that is giving me grief. The example
    > I've included demonstrates the problem (the code will make no sense i.e
    > why have friend functions when I'm not accessing private members etc but
    > the real code does actually have proper implementation). This code
    > should go in a header file then just build.
    >
    > //==============================================================
    > #ifndef _TEST_H_
    > #define _TEST_H_


    Why do you think you need the leading underscore for that macro? Drop
    it. Any name starting with an underscore and a capital letter is
    reserved by the implementation.

    >
    > #include <math.h>
    >
    > namespace sb
    > {
    > template<class T>
    > class CVector;
    >
    > template<class S>
    > CVector<S> pow(const CVector<S> & A, int n);
    >
    > template<class S>
    > void SVD(const CVector<S> A);
    >
    > template <class T>
    > class CVector
    > {
    > public:
    > CVector();
    > virtual ~CVector ();
    >
    > template<class S>
    > friend CVector<S> pow(const CVector<S> & A, int n);
    >
    > template <class S>
    > friend void SVD(const CVector<S> A);
    > };
    >
    > template<class T>
    > CVector<T> pow(const CVector<T> & A, int n)
    > {
    > CVector<T> S=A;
    > for (unsigned int i=1; i<n; i++)
    > {
    > S = S*A;
    > }
    > return S;
    > }
    >
    > template <class T>
    > void SVD(const CVector<T> A)
    > {
    > double result = pow(2.0,6.0);


    The only thing I can think of is that the presence of 'pow' functions
    (defined in your namespace and the class template 'CVector') is somehow
    interfering with the definition from <math.h>. Just for the hell of it,
    comment out all your 'pow' functions and see if it compiles.

    If my suspicions are correct, then add the scope resolution here:

    double result = ::pow(2.0,6.0);

    > }
    > }
    >
    > #endif
    > //==============================================================
    >
    > The error is:
    >
    > In file included from main.cpp:2:
    > test.h: In function ‘void sb::SVD(sb::CVector<S>)’:
    > test.h:44: error: no matching function for call to ‘pow(double, double)’
    >
    > It seems as if I can't use pow in math.h even though its included. Its
    > like my definition of pow is the only one. My compiler is gcc 4.2.5.
    >
    > Anyone got any hints to what is wrong?
    >


    Next time please post the *complete* compilable code example (see the
    FAQ 5.8).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 11, 2010
    #2
    1. Advertising

  3. Pierre Yves

    Balog Pal Guest

    "Pierre Yves"
    <>//==============================================================
    >
    > The error is:
    >
    > In file included from main.cpp:2:
    > test.h: In function ‘void sb::SVD(sb::CVector<S>)’:
    > test.h:44: error: no matching function for call to ‘pow(double, double)’


    Cameau C++ alse says:


    "ComeauTest.c", line 43: error: no instance of function template "sb::pow"
    matches
    the argument list
    The argument types that you used are: (double, double)
    double result = pow(2.0,6.0);

    You probably want to call ::pow or std::pow there. Or if overload set is
    meant you need using declarations.
    Balog Pal, Jan 11, 2010
    #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. Gianni Mariani
    Replies:
    1
    Views:
    345
    tom_usenet
    Sep 5, 2003
  2. Joe
    Replies:
    3
    Views:
    1,504
    Michael Tsang
    Oct 30, 2009
  3. A L
    Replies:
    1
    Views:
    506
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  4. Noah Roberts
    Replies:
    2
    Views:
    439
    Noah Roberts
    Mar 22, 2011
  5. Peter
    Replies:
    2
    Views:
    267
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page