The header file:
#include
using namespace std;
Template
The class ManyFriend
{
Private:
T item;
Public:
ManyFriend (const T & amp; I) : the item (I) {}
The template & lt; Typename C, typename D> Friend void show2 (C & amp; D & amp;);
};
The CPP file:
# include "ManyFriend. H"
Template
{
Cout & lt;
Mian function file:
# include "ManyFriend. H"
Int main ()
{
ManyFriend
ManyFriend
ManyFriend
Show2 (M1, M2);
Cout & lt;
system("pause");
return 0;
}
Error: 1:1 & gt; Source. Obj: error LNK2019: cannot resolve the external symbol of "void __cdecl show2 & lt; The class ManyFriend
2:1 & gt; Source. Obj: error LNK2019: cannot resolve the external symbol of "void __cdecl show2 & lt; The class ManyFriend
CodePudding user response:
At the end of the realization of the template class to write a fileCodePudding user response:
1, since you added a friend in front of the template function, this function is not a member of the template class, just a template function, so, the realization of it you should be on this template in front of the class, rather than on the CPP to
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
# # ifndef ManyFriendH
# define ManyFriendH
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#include
using namespace std;
TemplateVoid show2 (C & amp; C, D & amp; D)
{
Cout & lt;}
Template
The class ManyFriend
{
Private:
T item;
Public:
ManyFriend (const T & amp; I) : the item (I) {}
The template & lt; Typename C, typename D> Friend void show2 (C & amp; D & amp;);
};
# endif
CodePudding user response:
A class template declaration and definition, should be put. H, also is the header file, cannot be separated