#include
#include
using namespace std;
# define MAXSIZE 1001
The template & lt; The class T>
The class SqListClass {
T * data;
Int length;
Friend void Merge2 (SqListClass
{
Int I=0, j=0, k=0;
While (I & lt; A. ength & amp; & J & lt; B.l ength) {
If (a. d. ata [I]
L.d ata [k++]=a. d. ata [i++];
}
The else {
L.d ata [k++]=b.d ata [j++];
}
}
While (I & lt; A. ength)
L.d ata [k++]=a. d. ata [i++];
While (j & lt; B.l ength)
L.d ata [k++]=b.d ata [j++];
L.l ength=k;
}
}
Public:
SqListClass ();
~ SqListClass ();
Void CreateList (T a [], int n);
Int GetLength () {return length; }
Void DispList ();
};
The template & lt; Typename T>
SqListClass
Data=https://bbs.csdn.net/topics/new T [MAXSIZE];
Length=0;
}
//the constructor
The template & lt; Typename T>
SqListClass
The delete [] data;
}
The template & lt; Typename T>
Void SqListClass
int i;
For (I=0; I & lt; n; I++)
Data=[I] a [I];
Length=I;
}
The template & lt; Typename T>
Void SqListClass
int i;
If (length & gt; 0 {
For (I=0; I & lt; Length; I++)
{
if (i !=length - 1)
Cout & lt; The else cout & lt; }
}
}
Int main () {
Int z, I;
Cin & gt;> Z;
For (I=0; I & lt; Z; I++)
{
Int a [1001], [1001] b;
SqListClass
Int n, m, j;
Cin & gt;> n;
For (j=0; J & lt; n; J++)
{
Cin & gt;> A, [j].
}
P1. CreateList (a, n);
Cin & gt;> m;
For (j=0; J & lt; m; J++)
{
Cin & gt;> B [j];
}
P2. CreateList (b, m);
Merge2 (p3, p1, p2);
Cout & lt;
}
}
CodePudding user response:
Contains the friend keyword friend yuan statement is a statement of rights, not official declaration of function, outside the class definition a friend function Merge2 a solution is as follows,
The template & lt; The class T>
The class SqListClass
{
T * data;
Int length;
Public:
The template & lt; The class T>
Friend void Merge2 (SqListClass& The const SqListClass & The const SqListClass & amp;);//a friend declaration
};
The template & lt; The class T>
Void Merge2 (SqListClass& L, const SqListClass & A, const SqListClass & B)//function declaration
{
//TODO
}
Int main ()
{
SqListClassP1, p2, p3;
Merge2 (p3, p1, p2);//OK
}
CodePudding user response:
I wrote the compiler error directly soCodePudding user response:
I know, less statement when the template & lt; The class T>