0

クラス BSTNode をテンプレート化しました。

BSTNode.h

#ifndef _NODE_BST_H_    
#define _NODE_BST_H_

template <class K, class T>
struct BSTNode 
{
 typedef K keyType; 
 typedef T elementType;

 keyType _key; 
 elementType _element;  

 BSTNode<keyType,elementType>* _leftChild;            
 BSTNode<keyType,elementType>* _rightChild;


 // constructors
 BSTNode();   
 BSTNode<K,T>::BSTNode (keyType, elementType, unsigned int, BSTNode<K,T>*, BSTNode<K,T>*);

/*
some methods
*/ 

 BSTNode<K,T>& operator=(const BSTNode<K,T>& nodoBST2);
};

  template <class K, class T>
  BSTNode<K,T>::BSTNode ()
  {
   _leftChild=NULL;            
   _rightChild=NULL;

  }


  template <class K, class T>
  BSTNode<K,T>::BSTNode (keyType chiave, elementType elemento, unsigned int altezza, BSTNode* figlioSinistro=NULL, BSTNode* figlioDestro=NULL)
  {

    //constuctor code
  } 



  template <class K, class T>                  
  BSTNode<K,T>& BSTNode<K,T>::operator=(const BSTNode<K,T>& nodoBST2)
  {

    //operator= code

    return *this;       
}                 
#endif

main.c

#include <cstdlib>
#include <iostream>

#include "BSTnode.h"
using namespace std;

int main(int argc, char *argv[])
{
    BSTNode<string,string>* node1,node2;

    node1=NULL;
    node2=node1;

    system("PAUSE");
    return EXIT_SUCCESS;
}

エラーが発生します

no match for 'operator=' in 'node2 = node1' 
candidates are: BSTNode<K, T>& BSTNode<K, T>::operator=(const BSTNode<K, T>&) [with K = std::string, T = std::string]

必要な署名に一致する BSTNode クラスに operator= がありますが。

さらに、node1、node2 はクラス BSTNode へのポインターであるため、私の経験から、実際には operator= さえ必要ないことがわかっています。

何が問題なのですか?誰かが私を見て助けてくれますか?

お時間をいただきありがとうございます。

4

2 に答える 2

2
BSTNode<string,string>* node1,node2;

...として解析されます

BSTNode<string,string>* node1;
BSTNode<string,string> node2;

...*はタイプ名ではなく node1 にバインドされるためです。

あなたが書きたかったのはどちらかです

BSTNode<string,string>* node1, *node2;

また

BSTNode<string,string>* node1;
BSTNode<string,string>* node2;

後者は、将来そのような間違いをするのを防ぐため、明らかに優れています:)。

ポインターは=演算子から独立しているため、未加工のオブジェクトを割り当てたい場合を除き、定義は必要ありません。

于 2013-07-14T22:37:54.150 に答える
1

あなたはそれを知っていますか

BSTNode<string,string>* node1,node2;

と同等です

BSTNode<string,string>* node1;
BSTNode<string,string> node2;

?

認識している場合、正しい形式operator=はおそらく

 node2 = *node1; // where node1 != NULL;
 // Otherwise it should still compile but it leads to segmentation fault during run-time.

ポインターをコピーするだけの場合は、次のことを行う必要があります。

BSTNode<string,string>* node1;
BSTNode<string,string>* node2;
node2 = node1;
于 2013-07-14T22:40:46.553 に答える