2017-02-18 13 views
3

私はこの方法でテンプレートを使用しています。C++ typenameを使用して関数にtypedefを渡すテンプレート

#define MAX_CARS 999 
typedef char plate[20]; 

template <typename T> 
int insert_ord (T V[], int ll, int const LF, T e); 

これは機能しますが、引数としてtypedefを渡すと、次のようになります。 "insert_ord"を呼び出す関数がありません。

これがメインです。

int main (void) 
{ 
    plate targhe[MAX_CARS];   
    int ll = 0; 

    plate targa; 
    cin >> targa; 

    ll = insert_ord(targhe, ll, MAX_CARS, targa); 

    cout << endl; 
    system("pause"); 
    return 0; 
} 
+0

参照[「テンプレート内のポインタの配列の崩壊」]でデータ構造をラップ考える(http://stackoverflow.com/questions/1863751/array-decay-to-pointersイン・テンプレート)。タイプを保持するには、参照を使用する必要があります。 'int'を参照してください。' int'は 'insert_ord(T V []、int ll、int const LF、T&e)'です。 –

答えて

4
template <typename T> 
int insert_ord (T V[], int ll, int const LF, T e); 

機能、テンプレート上のは、型の引数のために動作しません:最後であるのに対し、あなたのinsert_ord機能の

(int[999][20], int, int, int[20]) 

最初のパラメータは、plateの2D配列でありますタイプplateであり、これは1Dアレイである。あなたは次のように関数テンプレートを宣言する必要があります。

template <typename T, int sizeOuter, int sizeInner> 
int insert_ord (T (&v)[sizeInner][sizeOuter], int ll, int const LF, T (&e)[sizeOuter]); 

はあなたのコードとそれLive On Coliru


しかし、いくつかのnitpicksを参照してください。 voidint main()には必要ありません。可能であれば、#defineよりもconstexprを好む。最も重要なのは、std::array<T, N>を使用するか、またはclassstruct

関連する問題