friend template in namespace error

P

Pierre Yves

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.
 
V

Victor Bazarov

Pierre said:
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
 
B

Balog Pal

"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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top