私は、次のコードがあります追加のテンプレートパラメータを持つ関数をコンパイル
ファイルmain.cppに:
#include <iostream>
#include "A.h"
class X{};
int
main()
{
X x;
A<X> a(x);
}
ファイルA-inl.h
#include <iostream>
template <class T1>
A<T1>::A(T1 t)
{
}
template <class T1>
void A<T1>::foo(T1 t1)
{
}
template <class T1, class T2>
void A<T1>::bar(T1 t, T2 t2) // Compilation Problem
{
}
ファイルああ
をtemplate <class T1>
class A
{
public:
explicit A(T1 t1);
void foo(T1 t1);
template <class T2>
void bar(T1 t1, T2 t2); // Compilation Problem
};
#include "A-inl.h"
g ++ -g main.cp p
In file included from A.h:11:0,
A-inl.h:14:6: error: prototype for ‘void A<T1>::bar(T1, T2)’ does not match any in class ‘A<T1>’
void A<T1>::bar(T1 t, T2 t2)
^
In file included from main.cpp:3:0:
A.h:8:10: error: candidate is: template<class T1> template<class T2> void A<T1>::bar(T1, T2)
void bar(T1 t1, T2 t2);
バーの宣言はどうすればいいですか? さらにもう1つの質問は、オブジェクトAを作成できるかどうかです。私はバーのT2のタイプを知らないのですか? barのコードはどのように生成されますか?
ありがとうございます。
returnステートメントがない場合、 'main'は暗黙的に0を返します。 – Brian
@Brianは、通常、VSを使って、mainを含む値を返す関数の戻り値がないときに問題を出します。あるいは、少なくとも警告や古いバージョンでは不平を言って、私はちょうど戻り値のうわべに入った。 –
なぜ賛成投票ですか? –