2016-07-23 7 views
-3

講師がクラスAvlTreeを定義するヘッダファイルを提供しましたが、なんらかの理由でmain()でオブジェクトを通常宣言できません。私は間違って何をしていますか?ここでは、ヘッダファイルの関連部分は次のとおりです。このヘッダファイルを使用してオブジェクトを宣言するにはどうすればよいですか? (テンプレート引数が見つからない)

#ifndef AVL_TREE_H 
#define AVL_TREE_H 

#include <iostream> // NULL, cin, cout 
using namespace std; 

template <typename Comparable> 
class AvlTree 
{ 
    public: 
    AvlTree() : root(NULL) 
     { } 
    AvlTree(const AvlTree & rhs) : root(NULL) 
    { 
     *this = rhs; 
    } 

private: 
    struct AvlNode 
    { 
    Comparable element; 
    AvlNode *left; 
    AvlNode *right; 
    int  height; 

    AvlNode(const Comparable & theElement, AvlNode *lt, 
              AvlNode *rt, int h = 0) 
     : element(theElement), left(lt), right(rt), height(h) { } 
    }; 

    AvlNode *root; 
}; 

#endif 

ここで私がメインでやろうとしているものです:

#include "AvlTree.h" 

void readFile(AvlTree &t1, AvlTree &t2) 
{ 
    // do some stuff 
    return; 
} 

void splayAccess(AvlTree &t1, AvlTree &t2) 
{ 
    // do some stuff 
    return; 
} 

int main (void) 
{ 
    // object declarations 
    AvlTree tree1; 
    AvlTree tree2; 

    // function calls 
    readFile(tree1, tree2); 
    splayAccess(tree1, tree2); 

    return 0; 
} 

そして、ここでは、エラー(GNUコンパイラ)です:

[email protected]:~/3110/hw4$ g++ header.h mcve.cpp 
mcve.cpp:3:15: error: variable or field ‘readFile’ declared void 
mcve.cpp:3:23: error: missing template arguments before ‘&’ token 
mcve.cpp:3:24: error: ‘t1’ was not declared in this scope 
mcve.cpp:3:36: error: missing template arguments before ‘&’ token 
mcve.cpp:3:37: error: ‘t2’ was not declared in this scope 
mcve.cpp:9:18: error: variable or field ‘splayAccess’ declared void 
mcve.cpp:9:26: error: missing template arguments before ‘&’ token 
mcve.cpp:9:27: error: ‘t1’ was not declared in this scope 
mcve.cpp:9:39: error: missing template arguments before ‘&’ token 
mcve.cpp:9:40: error: ‘t2’ was not declared in this scope 
mcve.cpp: In function ‘int main()’: 
mcve.cpp:18:10: error: missing template arguments before ‘tree1’ 
mcve.cpp:18:10: error: expected ‘;’ before ‘tree1’ 
mcve.cpp:19:10: error: missing template arguments before ‘tree2’ 
mcve.cpp:19:10: error: expected ‘;’ before ‘tree2’ 
mcve.cpp:22:11: error: ‘tree1’ was not declared in this scope 
mcve.cpp:22:18: error: ‘tree2’ was not declared in this scope 
mcve.cpp:22:23: error: ‘readFile’ was not declared in this scope 
mcve.cpp:23:26: error: ‘splayAccess’ was not declared in this scope 
+3

インストラクターが「using namespace std;」を含むヘッダーファイルを提供している場合は、あなたはこの任務より大きな問題を抱えています。 [あなたには無能なインストラクターがいる](http://stackoverflow.com/questions/18914106/what-is-the-use-of-using-namespace-std)。あなたは、このクラスに留まることによって、あなたの将来のキャリア見通しに不自由をしています。別のインストラクターを探してください。 "using namespace std;"ヘッダーファイル。良い悲しみ。逃げる。できるだけ早く。 –

+0

感想をお寄せいただきありがとうございますが、これは私の問題を解決するうえでは必ずしも役に立ちません。 –

+0

@Sam Varshavchik多かれ少なかれ正確に投稿しようとしていました。あなたは私にそれを打つ。 –

答えて

1
template <typename Comparable> 
class AvlTree 

// ... 

これは、AvlTreeというテンプレートクラスを宣言します。このテンプレートは1つのパラメータをとります。

void readFile(AvlTree &t1, AvlTree &t2) 

関数を宣言するためのシンタックスは、次のとおり

{return type} function-name({parameter list}) 

{parameter list}コンマで区切られたパラメータ関数のオプションのリストです。緩やかに言えば、各パラメータは

{type} {name} 

ように、その名前(再び、緩く話す)、続いて、パラメータの種類を、指定されています。

「AvlTree」はタイプではありません。それはテンプレートです。それを型にするには、適切なテンプレートパラメータを指定する必要があります。

void readFile(AvlTree<int> &t1, AvlTree<int> &t2) 

は今、あなたは、それぞれがタイプですAvlTree<int>、のインスタンスへの参照である、2つのパラメータを取る関数を宣言しました。 「AvlTree」自体はタイプではありません。これはテンプレートの名前です。

同じ問題が他のすべてのコンパイルエラーを引き起こしています。

ReadFile()のパラメータがAvlTree<int>のパラメータか、またはAvlTree<char>のパラメータか、それともそれ自体がテンプレート関数である必要があるかは、あなたが把握しておくべきものです。

+0

ありがとう、それは問題でした。知識が不足していることをお詫びします。私は2年間しかコーディングしていませんので、テンプレートに遭遇したのは初めてです。私は彼が要素の型名をマスクするためにそれを使用していると仮定したので、私が望むものを置くことができた。私はそれが私が望むものを明示的に定義しなければならないことを知らなかった。ヘッダーファイルと私のインストラクターの資格については、これはデータ構造とアルゴリズムのクラスであり、厳密にはコーディングではありません。私は彼が主にPythonで動作することを知っています。 –

関連する問題