2016-05-15 12 views
-2

どうすれば修正できますか?主な機能では、新しいノードを作成しようとすると文字列データがありますが、それはできません。エラーメッセージは、[エラー]は 'const std :: basic_string'を代入で 'int'に変換できません。私はどうすればいいのですか?C++エラー: 'const std :: basic_string <char>'を代入で 'int'に変換できません

template <typename value_type> 
class node : public element 
    public : 
    value_type data; 
    node(); 
    node (const value_type& T); 
    ~node(); 
}; 
template <typename value_type> 
node<value_type> :: node(const value_type& T) 
{ 
    type = 0; 
    intd= 0; 
    stringd = ""; 
    if(typeid(T)==typeid(int)) 
    { 
     type= 0;intd = T; 
    } 
    else if(typeid(T)==typeid(string)) 
    { 
     type = 3;stringd = T; 
    } 
    this->left = NULL; 
    this->right = NULL; 
    this->data = T; 
} 
int main() 
{ 
    string s1 = "123"; 
    node *n1 = new node<string>(s1); 
    return 0; 
} 
+1

を、 'そのためtypeid'をしてください使用しないでください。 ... 'type = 0; intd = T;'は条件が 'false'であってもまだコンパイルする必要があります。 – LogicStuff

答えて

0

問題は、この行である:

if(typeid(T)==typeid(int)) 
{ 
    type= 0;intd = T; // *** here *** 
} 

あなたは動的チェックtypeid(T)==typeid(int)があなたがint変数にTを割り当てる前に、C++は、入力された静的です。 stringint変数に割り当てることはできないため、割り当てはコンパイルされません。私も

  • class node : public element{を追加

    #include <string> 
    #include <typeinfo> 
    using std::string; 
    struct element{ 
        int type, intd; 
        string stringd; 
        void *left, *right; 
    }; 
    
    template <typename value_type> 
    class node : public element{ 
        public : 
        value_type data; 
        node(); 
        node (const value_type& T); 
        ~node(); 
    }; 
    
    template <typename value_type> 
    node<value_type> :: node(const value_type& T) 
    { 
        type = 0; 
        intd= 0; 
        stringd = ""; 
        this->left = NULL; 
        this->right = NULL; 
        this->data = T; 
    } 
    
    template <> 
    node<int> :: node(const int& T) 
    { 
        type= 0; 
        intd = T; 
        stringd = ""; 
        this->left = NULL; 
        this->right = NULL; 
        this->data = T; 
    } 
    
    template <> 
    node<string> :: node(const string& T) 
    { 
        type = 3; 
        intd= 0; 
        stringd = T; 
        this->left = NULL; 
        this->right = NULL; 
        this->data = T; 
    } 
    
    // defining destructor is required to use delete 
    template <typename value_type> 
    node<value_type> :: ~node() 
    { 
    } 
    
    int main() 
    { 
        string s1 = "123"; 
        node<string> *n1 = new node<string>(s1); 
        delete n1; 
        return 0; 
    } 
    

    代わりに、テンプレートの特殊化を使用することができます。

  • n1nodeからnode<string>に変更しました。コンパイルエラーを避けるためです。
0

最終的には、Nodeオブジェクトをリンクリストのような何らかのコンテナに入れたいと思うでしょう。コンテナは異なるタイプの要素を処理しません。 void *ポインタをコンテナに格納して、内部の詳細を隠すことができます。しかし、コンパイラの型チェックをやめてしまいます。

タイプチェックを戻す1つの方法は、boost :: variantのようなクラスで複数の型を保持する仕組みを隠すことです。あなたのようにdataを宣言するためにあなたのNodeオブジェクトは、テンプレート化する必要はありません。

boost::variant<int, string> data; 

boost::variant<int, string>のみintstringを処理しますが、あなたはより多くの種類を処理するためのより多くのテンプレートパラメータ、例えばboost::variant<int, string, double>を追加することができます。

あなたがこの困難な問題を処理する方法のボンネットの下を見に興味があるなら、フォルカー・シモニスとローランド・ウェイス、この偉大な独創記事をチェックアウト:http://www.progdoc.de/papers/nseq/nseq/nseq.html

関連する問題