Home > Back-end >  Ask bosses, why do I write a friend function Merge2 said outside the class definition error when the
Ask bosses, why do I write a friend function Merge2 said outside the class definition error when the

Time:11-23

Ask bosses, why do I write a friend function Merge2 said outside the class definition error when they cannot resolve the external symbol, and in the class can run normally
#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 & L, const SqListClass & A, const SqListClass & B) {
{
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 : : SqListClass () {
Data=https://bbs.csdn.net/topics/new T [MAXSIZE];
Length=0;
}
//the constructor

The template & lt; Typename T>
SqListClass : : ~ SqListClass () {
The delete [] data;
}


The template & lt; Typename T>
Void SqListClass : : CreateList (T a [], int n) {
int i;
For (I=0; I & lt; n; I++)
Data=[I] a [I];
Length=I;
}


The template & lt; Typename T>
Void SqListClass : : DispList () {
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 P1, p2, p3;
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; P3. DispList ();
}
}


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 ()
{
SqListClass P1, p2, p3;
Merge2 (p3, p1, p2);//OK
}

CodePudding user response:

I wrote the compiler error directly so

CodePudding user response:

I know, less statement when the template & lt; The class T>
  • Related