Template Friend Function

D

Dmitry D

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;
}
 
O

Oplec

Dmitry said:
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.
 

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

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top