Home > Back-end >  Write a class template library need
Write a class template library need

Time:09-24

The code is very simple;
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 Void show2 (C & amp; C, D & amp; D)
{
Cout & lt; }

Mian function file:
# include "ManyFriend. H"

Int main ()
{
ManyFriend M1 (10);
ManyFriend M2 (20);
ManyFriend The M3 (10.5);
Show2 (M1, M2);
Cout & lt; Show2 (M2, M3);
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 The class ManyFriend (class ManyFriend & The class ManyFriend & amp;) "(?????? $show2 @ V? @ @ $ManyFriend @ H V1 @ @ @ YAXAAV? $ManyFriend @ @ @ @ 0 H Z), the symbol referenced in the function _main
2:1 & gt; Source. Obj: error LNK2019: cannot resolve the external symbol of "void __cdecl show2 & lt; The class ManyFriend The class ManyFriend (class ManyFriend & The class ManyFriend & amp;) "(?????? $show2 @ V? @ @ $ManyFriend @ H V? $ManyFriend @ N @ @ @ @ YAXAAV? $@ @ AAV ManyFriend @ H? @ @ @ $ManyFriend @ N Z), the symbol referenced in the function _main

CodePudding user response:

At the end of the realization of the template class to write a file

CodePudding 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;
Template Void 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
  • Related