Home > Back-end >  Template class constraint template function problem
Template class constraint template function problem

Time:03-27

Procedure 1:
#include
Using STD: : cout;
Using STD: : endl;
Template Void show2 (T & amp; Hf) {STD: : coutTemplate
Class A
{
Private:
int a;

Public:
Friend void show2 & lt; T> (A & amp;);
};


The template class A;
Int main (void)
{

return 0;
}
Results: the error


Program 2:
#include
Using STD: : cout;
Using STD: : endl;
Template Void show2 (T & amp; Hf) {STD: : coutTemplate
Class A
{
Private:
int a;

Public:
Friend void show2 & lt; T> (A & amp;);
};


//the template class A;
Int main (void)
{

return 0;
}
Results: no error

Is the difference between the two procedures: there are explicit instantiation is an error, otherwise we are not an error, what reason is this?

CodePudding user response:

Your explicit instantiation grammar is wrong?

CodePudding user response:

Both are wrong, the second is not an error because you haven't instantiate
Template class a friend is such statement
 # include 
#include
using namespace std;

Template
Class A
{
Private:
int a;

Public:
A () {}
Template
Friend void show2 (A & amp;);
};


Template
Void show2 (A & Hf)
{
STD: : cout & lt; }
Template<>
The class A {
Double a;
Public:
(A) {A=0; }
Template
Friend void show2 (A & amp;);
};
Int main (void)
{
A A1.
Show2 (a1);
return 0;
}

  • Related