How can I declare and define a friend template function in a template class? | | |
Hi folks,
I am running into with such a question when I tried to declare and
define a friend template function in a template class, here is the
code snippet:
#include <iostream>
using namespace std;
template<typename T>
class Test {
friend Test<Tindex(Test<T>& start, Test<T>& end, Test<T>& step);
};
template<typename T>
Test<Tindex(Test<T>& start, Test<T>& end, Test<T>& step) {
cout << "This is index is working\n";
return Test<T>();
}
int main() {
Test<intstart = Test<int>();
Test<intend = Test<int>();
Test<intstep = Test<int>();
Test<intrslt = index(start, end, step);
}
When compiled with VC8.0, a linkage error araised:
Test.obj : error LNK2019: unresolved external symbol "class TVEC<int>
__cdecl index(class TVEC<int&,class TVEC<int&,class TVEC<int>
&)" (?index@@YA?AV?$TVEC@H@@AAV1@00@Z) referenced in function _main | | | | re: How can I declare and define a friend template function in a template class?
Αυκ» wrote: Quote:
Hi folks,
I am running into with such a question when I tried to declare and
define a friend template function in a template class, here is the
code snippet:
>
#include <iostream>
using namespace std;
>
template<typename T>
class Test {
>
friend Test<Tindex(Test<T>& start, Test<T>& end, Test<T>& step);
Since 'index' is a template, it has to be declared at the namespace level
before it's declared here. Otherwise, this friend declaration refers to
a non-template function.
Take the declaration of 'index' from below (not the whole definition)
and place it before the template definition. That will require the
template itself to be *declared* before it, as well. I know, I know,
it's a PITA, but that's what is required. Quote:
};
>
template<typename T>
Test<Tindex(Test<T>& start, Test<T>& end, Test<T>& step) {
cout << "This is index is working\n";
return Test<T>();
}
>
>
int main() {
>
Test<intstart = Test<int>();
Test<intend = Test<int>();
Test<intstep = Test<int>();
>
Test<intrslt = index(start, end, step);
}
>
When compiled with VC8.0, a linkage error araised:
>
Test.obj : error LNK2019: unresolved external symbol "class TVEC<int>
__cdecl index(class TVEC<int&,class TVEC<int&,class TVEC<int>
&)" (?index@@YA?AV?$TVEC@H@@AAV1@00@Z) referenced in function _main
This is what your code should look like (I think):
#include <iostream>
using namespace std;
template<typename Tclass Test;
template<typename T>
Test<Tindex(Test<T>& start, Test<T>& end, Test<T>& step);
template<typename T>
class Test {
... // as before
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | | | re: How can I declare and define a friend template function in a template class?
First, thank you for your informative tips, I rewrite my code snippet,
here it is:
#include <iostream>
using namespace std;
template<typename T>
class Test;
template<typename T>
Test<Tindex(T start, T end, T step);
template<typename T>
class Test {
public:
friend Test<Tindex(T start, T end, T step);
};
template<typename T>
Test<Tindex(T start, T end, T step) {
cout << "This is index is working\n";
return Test<T>();
}
int main() {
int start;
int end;
int step;
index(start, end, step);
}
It's the same as you think it is supposed to be, no problem with
compling and linking, but at runtime, a error is raised. | | | | re: How can I declare and define a friend template function in a template class?
Αυκ» wrote: Quote:
First, thank you for your informative tips, I rewrite my code snippet,
here it is:
>
#include <iostream>
using namespace std;
>
template<typename T>
class Test;
>
template<typename T>
Test<Tindex(T start, T end, T step);
>
template<typename T>
class Test {
public:
friend Test<Tindex(T start, T end, T step);
};
>
template<typename T>
Test<Tindex(T start, T end, T step) {
cout << "This is index is working\n";
return Test<T>();
}
>
int main() {
>
int start;
int end;
int step;
>
index(start, end, step);
}
>
It's the same as you think it is supposed to be, no problem with
compling and linking, but at runtime, a error is raised.
What error do *you* get?
You're passing 'start', 'end', 'step' into the function _without_
_giving them any value_. The lvalue-to-rvalue conversion required
for argument passing causes the program to have *undefined behaviour*.
Try initialising all your variables. Or just call
index(1, 2, 3);
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask | | | | re: How can I declare and define a friend template function in a template class?
Forget about the runtime error, it's because I used 3 variables before
I define them, sorry about that, it's OK when I initialize them. But a
new linkage error show itself when I tried to assign the return value
of index to another Test<Tobject, here is the code snippet:
#include <iostream>
using namespace std;
template<typename T>
class Test;
template<typename T>
Test<Tindex(T start, T end, T step) {
cout << "This is index is working\n";
return Test<T>();
}
template<typename T>
class Test {
public:
friend Test<Tindex(T start, T end, T step);
};
int main() {
int start = 1;
int end = 32;
int step = 1;
Test<intrslt = index(start, end, step);
}
The error is:
Test.obj : error LNK2019: unresolved external symbol "class Test<int>
__cdecl index(int,int,int)" (?index@@YA?AV?$Test@H@@HHH@Z) referenced
in function _main | | | | re: How can I declare and define a friend template function in a template class?
should be;
template<typename T>
class Test {
public:
template<typename T//look here
friend Test<Tindex(T start, T end, T step);
}; | | | | re: How can I declare and define a friend template function in a template class?
Hi folks,
Thanks for your informative tips, both of your answers can solve my
problem successfully. But I have a question coming into my mind:
If there is any difference between:
template<typename T>
class Test {
friend Test<Tindex<>(T, T, T);
// ^^^
};
and
class Test {
friend Test<Tindex<T>(T, T, T);
// ^^^
};
I know you've explained it, but how can I prove that they are indeed
different, thank you. | | | | re: How can I declare and define a friend template function in a template class? leomayleomay@gmail.com wrote: Quote:
Thanks for your informative tips, both of your answers can solve my
problem successfully. But I have a question coming into my mind:
>
If there is any difference between:
>
template<typename T>
class Test {
friend Test<Tindex<>(T, T, T);
// ^^^
};
>
and
>
class Test {
friend Test<Tindex<T>(T, T, T);
// ^^^
};
>
I know you've explained it, but how can I prove that they are indeed
different, thank you.
I am not sure they are different, to be honest. The <Tafter 'index'
is actually implied by the empty angle brackets.
Now, if you were to write
template<class T>
class Test {
template<class Ufriend Test<Uindex(U,U,U);
};
That would definitely be different. It means that _any_ instantiation
of the 'index' template is a friend of this instantiation of 'Test'.
The usefulness of that is questionable, but it would allow you to do
template<class STest<Sindex(S s1, S s2, S s3)
{
Test<blalblahsomeOtherTest;
// and then access 'someOtherTest's privates
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|