C++では、子クラスまたはフレンド関数が、親クラスのすべてのテンプレートタイプにどのようにアクセスできるのでしょうか?どのようにタイプT
であっても、friend関数と子関数は型エラーを起こさないように、以下のコードをどのように変更すればよいですか? (現在、intの型だけが適切に動作します)。C++テンプレートクラスの子/友人にテンプレートを適用するにはどうすればよいですか?
// PARENT CLASS:¨
template <class T>
class parent{
public:
T variable1;
friend int function1(parent, int a);
};
//CHILD CLASS:
class child : public parent<int> {
public:
void setvariable(int a){ variable1 = a; };
};
// FRIEND FUNCTION:
int function1(parent<int> k, int a) {
return k.variable1 +a;
};
だから、その後エラーなしでコンパイルします以下:
int main() {
child child1; //Child
child child2;
child1.setvariable(4);
child2.setvariable(4.4); //Type error/retyping
cout << function1(child1.variable1, 4) << endl; // Function
cout << function1(child2.variable1, 4.0) << endl; // Type error
system("pause");
return 1;
}
[テンプレートクラスのテンプレートフレンド機能の宣言]の可能な複製(https://stackoverflow.com/questions/18792565/declare-template-friend-function-of-template-class) – AndyG
コードの例を表示できますかあなたは持っていたいですか?たとえば、function1の定義ですか?私には、あなたが求めているものは不明です。 – Jonas
@ Jonas関数の定義は、2行目から最後の行にあります。 – Tony