次のコードがあります
Classes.h
#ifndef CLASSES_H
#define CLASSES_H
#include <iostream>
using namespace std;
template< class T1, class T2>
class class1
{
public:
virtual void method1(int) const =0;
virtual void method2(class1&) const =0;
};
template< class T1>
class class2:public class1<T1,int>
{
public:
void method1(int) const;
void method2(class2&) const;
};
template< class T1>
void class2<T1>::method1(int i) const
{
cout<<"class2::method1 - before Call %i"<<endl;
cout<<"class2::method1 - after Call"<<endl;
}
template< class T1>
void class2<T1>::method2(class2& c2) const
{
cout<<"class2::method2 - before Call"<<endl;
cout<<"class2::method2 - after Call"<<endl;
}
#endif
main.cpp
#include <cstdlib>
#include <iostream>
using namespace std;
#include "Classes.h"
int main(int argc, char *argv[])
{
class2<int> c2;
c2.method1(0);
c2.method2(c2);
system("PAUSE");
return EXIT_SUCCESS;
}
基本的に、C1 はインターフェイス クラスであるため、そのメソッドは純粋に仮想です。発生している問題は、Medhod2 がクラス自体のインスタンス (インターフェイスの場合は class1、そのようなインターフェイスを実装するクラスの場合は class2) を渡すことです。
したがって、Method2 には署名があります
void method2(class1&) const;
class1 で
void method2(class2&) const;
クラス2で。
これが、コンパイル時に以下のエラーが発生する理由です。
main.cpp: In function `int main(int, char**)':
main.cpp:12: error: cannot declare variable `c2' to be of type `class2<int>'
main.cpp:12: error: because the following virtual functions are abstract:
Classes.h:14: error: void class1<T1, T2>::method2(class1<T1, T2>&) const [with
T1 = int, T2 = int]
make: *** [main.o] Error 1
この問題を解決するにはどうすればよいですか?
誰か教えてください。ありがとう。