473,398 Members | 2,165 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,398 software developers and data experts.

Template Friend Function

Hi all,
I'm having problems with declaring a template friend function. It seems like
I've done everything as explained
in C++ FAQ, but still, I'm getting the linker error (unresolved external
'aFunc(A<char> const&)' ) in
the following sample program. Tried it on GCC and Borland's compiler.
Can somebody please show me the correct way to fix this?

Thanks,
Dmitry

// all the following code is located in a single source file
#include <iostream>
template<typename T> class A; // forward class declaration
template<typename T> void aFunc(const A<T>&);

template <typename T>
class A
{
public:
A(T _object): value(1), object(_object) {}
T& GetObject() { return object; }

friend void aFunc(const A<T>&);
private:
T object;
int value;
};

// friend function
template <typename T>
void aFunc(const A<T>& container)
{
std::cout << "value: " << container.value;
}

int main()
{
A<char> myClass('a');
aFunc(myClass);
return 0;
}
Jul 19 '05 #1
1 4414
Dmitry D wrote:
Hi all,
I'm having problems with declaring a template friend function. It seems like
I've done everything as explained
in C++ FAQ, but still, I'm getting the linker error (unresolved external
'aFunc(A<char> const&)' ) in
the following sample program. Tried it on GCC and Borland's compiler.
Can somebody please show me the correct way to fix this?

Thanks,
Dmitry

// all the following code is located in a single source file
#include <iostream>
template<typename T> class A; // forward class declaration
template<typename T> void aFunc(const A<T>&);

template <typename T>
class A
{
public:
A(T _object): value(1), object(_object) {}
T& GetObject() { return object; }

friend void aFunc(const A<T>&);
private:
T object;
int value;
};

// friend function
template <typename T>
void aFunc(const A<T>& container)
{
std::cout << "value: " << container.value;
}

int main()
{
A<char> myClass('a');
aFunc(myClass);
return 0;
}


Hi, I asked this question a few days ago. I was trying it the way you
were too. The following is what I learned on declaring friends in
templates: You need to add <> in the declaration, but not in the definition.

- Declaring a class template to be a friend takes the form:
template<class ...> friend class ...;

- Declaring an operator to be a friend takes the form:
friend ... operator... <>(...);
I have not tried to declare a regular function to be a friend, but I
assume you should use "friend void aFunc<>(const A<T>&);"

Since aFunc is a template, you have to tell the compiler that it is in
the declaration. You might try "template<class T> friend void
aFunc()(const A<T>&);"

I'm new to this too, and hope that I've been of help.

Jul 19 '05 #2

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

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
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: ...
3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
5
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T>...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination with mingw because this code (or at least code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.