-1

コードブロック バージョン 10.05 に問題があります。C++ プロジェクトを作成し、次のようなプログラムを作成しました。

main.cpp

#include <iostream>
#include "vectorddd.hpp"


using namespace std;

int main()
{

    vector3D<int> tesztinttomb;
    tesztinttomb.saveout("igen.dat");
    return 0;
}

ヘッダー ファイル (vectorddd.hpp):

#ifndef VECTORDDD_HPP_INCLUDED
#define VECTORDDD_HPP_INCLUDED

#include <iostream>

template <class T>
class vector3D  {
    T *x;
    T *y;
    T *z;
    int meret;
public:
    void saveout(char* FileName);

    vector3D(int Meret=0) :  x(new T[meret]), y(new T[Meret]), z(new T[Meret]), meret(Meret) {}

    ~vector3D()  { delete [] x; delete [] y; delete [] z; }
};


#endif // VECTORDDD_HPP_INCLUDED

実装ファイル (vectorddd.cpp):

#include "vectorddd.hpp"

template <class T>
void vector3D<T>::saveout(char* FileName) {
    int i=0;// I know this is stupid... but the emphasis is on the linking problem

}

そして、それは互いにリンクしていません。.cpp ファイルのリンクを確認し、[プロパティ] -> [ビルド オプション] で設定をコンパイルする必要があることはわかっています。そして、私は何の問題も見つけませんが、常に同じように書いています:

In function `main':
undefined reference to `vector3D<int>::saveout(char*)'
||=== Build finished: 1 errors, 0 warnings ===|

そして、.cpp ファイルの実装を .hpp ファイルに入れると、正しく動作します。しかし、これはコードブロックが機能する方法ではありません。

4

1 に答える 1

2

テンプレートはヘッダー ファイルにある必要があります。考えてみてください。cpp ファイルにある場合、テンプレートをどのようにインスタンス化できますか?

あなたはこれを置くべきです:

template <class T>
void vector3D<T>::saveout(char* FileName) {
    int i=0;// I know this is stupid... but the emphasis is on the linking problem

}

ヘッダーの vectorddd.hpp ファイルに

同様の SO 投稿を参照してください: C++ テンプレート関数定義を .CPP ファイルに保存する

于 2012-04-19T19:40:59.433 に答える