次のコードをコンパイルするときにこのエラーが発生しました。 いくつかの研究を行い、さまざまな状況で同様のエラーを読んだ後、私は必要な解決策を考え出しました。 しかし、私はエラーと修正のための激しい理由を完全に理解していませんでした。このコードをコンパイルするときテンプレート継承:テンプレートパラメータに依存する引数がありません
がtemplate <typename T>
class TestA {
int a;
T temp;
protected:
int b;
public:
int c;
TestA(T te): a{10}, b{20}, c{30}, temp{te} {}
int geta(){ return a; }
int getb(){ return b; }
int getc(){ return c; }
};
template <typename T>
class TestB {
int atb;
T tempb;
protected:
int btb;
public:
int ctb;
TestB(T te) atb{10}, btb{20}, ctb{30}, tempb{te} {}
};
template <typename T>
class TestInh : public TestA<T>, public TestB<T> {
int aa;
T temptemp;
protected:
int bb;
int b;
public:
int cc;
TestInh(T te) : TestA<T>{te}, TestB<T>{te}, bb{10000}, b{-1000} {}
int get_total() {
// The error happens here!
return geta();
}
};
int main(int argc, char const *argv[]) {
char text = 'a';
TestInh<char> test(text);
//std::cout << test.geta() << std::endl;
std::cout << test.get_total() << std::endl;
//std::cout << test.c << std::endl;
return 0;
}
、私はこのエラーを得た:
testtemplate.cc: In member function ‘int TestInh<T>::get_total()’:
testtemplate.cc:54:32: error: there are no arguments to ‘geta’ that depend on a template parameter, so a declaration of ‘geta’ must be available [-fpermissive]
int get_total() {return geta();}
^
testtemplate.cc:54:32: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
それはthis->geta()
を呼び出すだけではなくgeta()
によって解決されるが、これは、コンパイラによって解決することができない理由を私は完全には理解していません。
誰かが私の理由を説明できますか?
それは@Gpg MSVCは、間違っているVS2015でコンパイル – GpG
:
関数はメンバ関数として存在してコンパイラに言うのもう一つの方法は、usingステートメントを追加することです。その中に 'template'というキーワードがあるものは、それに頼らないでください。 –