Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > friend template in namespace error

Reply
Thread Tools

friend template in namespace error

 
 
Pierre Yves
Guest
Posts: n/a
 
      01-11-2010
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-11-2010
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 = :ow(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..

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Balog Pal
Guest
Posts: n/a
 
      01-11-2010

"Pierre Yves"
<(E-Mail Removed)>//================================================== ============
>
> 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:ow"
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 :ow or std:ow there. Or if overload set is
meant you need using declarations.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
friend template function in distant namespace Noah Roberts C++ 2 03-22-2011 12:05 AM
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
help with namespace/friend error Joe C++ 3 10-30-2009 10:26 AM
using friend function template in class template Yueh-Wei Hu C++ 0 05-23-2004 11:36 AM
Template friend can't be template parameter Gianni Mariani C++ 1 09-05-2003 09:15 AM



Advertisments