Re: template class, methods and friend, unable to link
suresh.amritapuri wrote:
On Dec 18, 2:20 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
suresh.amritapuri wrote:
[..]
Let's make it very simple to start.
[..]
thanks Victor, I was able to do the same with non friend functions. I
mean I could successfully compile and link. Linker failed only in the
friend function case. Your example has only non friend functions....
Did you even try?
------------------------------------------------ foo.h
template<class T> void foo(T t); // declaration
template<class T> class Foo
{
private:
friend void foo<>(T); // here is the friend declaration
Foo(T);
};
------------------------------------------------ foo.cpp
#include "foo.h"
#include <typeinfo>
#include <iostream>
template<class T> void foo(T t) // definition
{
Foo<T> ft(t); // should be fine for all - we're friends
std::cout << "foo(" << typeid(ft).name() << ")\n";
}
template<class T> Foo<T>::Foo(T)
{
std::cout << "Constructor Foo(" << typeid(T).name() << ")\n";
}
template void foo<int>(int); // explicit instantiation
template void foo<double>(double); // explicit instantiation
template void foo<char>(char); // explicit instantiation
------------------------------------------------ main.cpp
#include "foo.h"
int main()
{
foo(666);
foo(6.66);
foo('L');
}
----------------------------------------------------------
In the example above even the Foo c-tor is in a C++ file since it's not
used from 'main'...
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask