私はこのコードを2つの異なるクラスで同じテンプレートを使用しようとしています。私はコンパイルするとき、私はエラーを取得:異なるクラスに同じテンプレートを使用できないのはなぜですか?
#include <iostream>
#include <memory>
template <class T>
class Node
{
public:
int value;
std::shared_ptr<Node> leftPtr;
std::shared_ptr<Node> rightPtr;
Node(int val) : value(val) {
std::cout<<"Contructor"<<std::endl;
}
~Node() {
std::cout<<"Destructor"<<std::endl;
}
};
template <class T>
class BST {
private:
std::shared_ptr<Node<T>> root;
public:
void set_value(T val){
root->value = val;
}
void print_value(){
std::cout << "Value: " << root.value << "\n";
}
};
int main(){
class BST t;
t.set_value(10);
t.print_value();
return 1;
}
エラー:
g++ -o p binary_tree_shared_pointers.cpp --std=c++14
binary_tree_shared_pointers.cpp:39:8: error: elaborated type refers to a template
class BST t;
^
binary_tree_shared_pointers.cpp:21:7: note: declared here
class BST {
^
'root.value'または' root-> value'? –
'class BST t;'あなたはCから来ていますか? 1)それは正当ではないC++で、関連しないメモでも 'struct'は必要ないが、許されている2)テンプレートのパラメータを指定する必要がある – Rakete1111
あなたが尋ねたエラーについては、テンプレート化されたクラスを使用しますか?あなたは 'Node'テンプレートでそれを行いますか?おそらくあなたはいくつかのステップを取って、[良い初心者の本を見つける](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)読むべきですか? –