default argument in friend function of template class

X

xuatla

The following is just a sample code to demostrate my question:

-----------
template <typename T>
class C
{
public:
friend void f1(double i=2) { std::cout << i; } ;
};

-----------
compile result:
test1.cpp: In instantiation of `void f1(double)':
test1.cpp:95: instantiated from here
test1.cpp:88: error: default argument given for parameter 1 of `void
f1(double)'
test1.cpp:88: error: after previous specification in `void f1(double)'
------------

If I don't use template or I remove the default argument in f1, then no
problem occurs. But with both of them it won't work.

Anyone know the reason for this problem?

Thanks a lot in advance.

X
 
V

Victor Bazarov

xuatla said:
The following is just a sample code to demostrate my question:

-----------
template <typename T>
class C
{
public:
friend void f1(double i=2) { std::cout << i; } ;

This defines a [global] function 'f1', and as soon as you try to
instantiate the template more than once, you get multiple definition
of 'f1'.
};

-----------
compile result:
test1.cpp: In instantiation of `void f1(double)':
test1.cpp:95: instantiated from here
test1.cpp:88: error: default argument given for parameter 1 of `void
f1(double)'
test1.cpp:88: error: after previous specification in `void f1(double)'

Of course it won't. Did you mean to define a template function 'f1'?
Anyone know the reason for this problem?

Yes, see above. Of course, it's unclear what you're trying to achieve,
but here is a potential solution:

#include <iostream>

template<class T> void foo(double = 2) { std::cout << i; }

template<class T>
class C
{
public:
friend void foo<T>(double i);
};

int main() {
C<char> cc;
C<int> ci;
}


V
 
X

xuatla

Thank you, Victor.

Victor said:
xuatla said:
The following is just a sample code to demostrate my question:

-----------
template <typename T>
class C
{
public:
friend void f1(double i=2) { std::cout << i; } ;


This defines a [global] function 'f1', and as soon as you try to
instantiate the template more than once, you get multiple definition
of 'f1'.
see below.
Of course it won't. Did you mean to define a template function 'f1'?
I mean function 'f1' is a friend of the template class 'C'.
It was not clear in my former post. But what I want to do is
friend void f1(const C& c, double i=2);

Here 'f1' has default argument 'i' and it calls some private members of
class 'c'.

For you above multiple definition of 'f1': if I add c as an argument of
'f1', then for different instantiate of class C, 'f1' has different
arguments so it should not have the confliction. Am I correct?
Yes, see above. Of course, it's unclear what you're trying to achieve,
but here is a potential solution:

#include <iostream>

template<class T> void foo(double = 2) { std::cout << i; }

template<class T>
class C
{
public:
friend void foo<T>(double i);
};

int main() {
C<char> cc;
C<int> ci;
}


V

This way works. Thank you. 'f1' can get access to the private members.
Thanks a lot!

X
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top