2011-08-12 8 views
2

私はAndroid NDK r6とAndroid SDK API 8(2.2)を使用しています。AndroidのC++テンプレートNDK

私はテンプレートを使ってstd :: vectorのような動的リストを実装しようとしていますが、コンパイルされた.oファイルには多くのエラーがあります。

サンプル:

error

にエラーがないソースファイルで、コンパイルされたファイルの.oで生産されて見ることができるように。

クラス定義:

template <class T> 
class ArrayList{ 
    private: 
     int mSize; 

    public: 
     /** 
     * Construye una lista dinámica vacía. 
     */ 
     ArrayList(); 

     /** 
     * Destructor. 
     */ 
     ~ArrayList(); 

     /** 
     * Añade un elemento a la lista. 
     * 
     * @param element 
     *    Elemento. 
     */ 
     void add (T element); 

     /** 
     * Obtiene un elemento de la lista. 
     * 
     * @param index 
     *    Índice del elemento. Rango válido de valores: [0, size()] 
     * @return Elemento de la posición indicada o NULL si el índice no es válido. 
     */ 
     T get (int index); 

     /** 
     * Elimina un elemento de la lista. 
     * 
     * @param index 
     *    Índice del elemento. Rango válido de valores: [0, size()] 
     */ 
     void remove (int index); 

     /** 
     * Vacía la lista. 
     */ 
     void clear(); 

     /** 
     * Consulta el número de elementos de la lista. 
     * 
     * @return Número de elementos. 
     */ 
     int size(); 

     /** 
     * Consulta si la lista esta vacía. 
     * 
     * @return true si está vaía, sinó false. 
     */ 
     bool isEmpty(); 
}; 

クラスの実装:

template <class T> 
ArrayList<T>::ArrayList(){ 
    mSize = 0; 
} 

template <class T> 
ArrayList<T>::~ArrayList(){ 

} 

template <class T> 
void ArrayList<T>::add (T element){ 

} 

template <class T> 
T ArrayList<T>::get (int index){ 
    T element; 
    return element; 
} 

template <class T> 
void ArrayList<T>::remove (int index){ 

} 

template <class T> 
void ArrayList<T>::clear(){ 

} 

template <class T> 
int ArrayList<T>::size(){ 
    return mSize; 
} 

template <class T> 
bool ArrayList<T>::isEmpty(){ 
    return true; 
} 

クラスの使用法:それは

ArrayList<OtherClass> list; 
OtherClass foo; 
list.add (foo); 
+0

「C++」タグが適切なものとして追加されました。 – Mahesh

答えて

6

テンプレートコードです。あなたは、通常のC++クラスのように、cppファイル関係と同じヘッダファイルを持っていません。

がfoo.h:私は一般的にそうようにそれを再構築

#ifndef FOO_H 
#define FOO_H 

template <class T> 
class Foo 
{ 
    Foo(); 
}; 

// Note that the header file INCLUDES the cpp file. This is simply to maintain 
// the general .h .cpp file structure, but adapt it to template code, where the 
// implementation is supposed to be in the header file and is not compiled. 
#include "Foo.cpp" 

#endif 

Foo.cpp:

// Note that I do NOT include the header here. Also, do NOT compile this file. 
// So if you have a makefile, be sure not to include this file in it. 
template <class T> 
Foo<T>::Foo() 
{ } 

それとも、ただのcppを持つヘッダファイルで全体の実装を固執することができず、ファイルはまったくありません。これは実際には、cppファイルをコンパイルしないので意味があります。続きを読むhere

2

リンカーエラーです。コンパイラがメンバ関数のコードをインスタンス化するためには、メンバ関数定義をヘッダ自体に入れなければなりません。

+0

クラス定義はArrayList.hであり、クラス実装ArrayList.cppです。何を言っているのかわかりません。 –

+0

実際には、テンプレートにも同様にヘッダーに実装を入れる必要があります。 –

+1

@GagleKasそしてこれは間違いです。テンプレートの宣言と定義を分ける良い方法はありません。 http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12 – pmr