473,395 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

default argument in friend function of template class

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
Sep 21 '05 #1
2 2486
xuatla wrote:
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)'
------------

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.
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
Sep 21 '05 #2
Thank you, Victor.

Victor Bazarov wrote:
xuatla wrote:
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.
};

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

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?
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


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

X
Sep 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: CoolPint | last post by:
Can anyone explain how I can make the following function accept an default arguement for the last parameter, which should be an optional functor? template <typename T, typename FUNCTOR> void...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
2
by: Gary | last post by:
Hi, I am a Chinese student, I have a problem with the following code //The follwing code in StaticSearch.h: // template <class Type> class dataList; // template <class Type> class Node ...
4
by: Justin Miller | last post by:
Ok, I tried to make that subject as descriptive as possible. What I'm trying to do: I'm attempting to use policies to create a generic memento (design pattern) template. My Memento template so...
3
by: Capstar | last post by:
Hi NG, I am trying to get the attached piece of code to work, but I can't figure out what I'm doing wrong. To me it seems that when I don't pass an argument to x::do_something, it should use the...
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
2
by: =?gb2312?B?wfXquw==?= | last post by:
Hi folks, I am trying to add a default argument to a template function in a template class, here is the code snippet: template<typename T> class Test { template<typename U> friend void...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
2
by: Clyde | last post by:
Hi, what i'm trying to do is: /////////////// Code Start template <class TType, int* p = 0> class Template { public:
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.