0

I have templated class 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;
}

I get error

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]

even thoug I have operator= in BSTNode class matching the required signature.

Moreover, being node1, node2 pointers to class BSTNode, from my experience, I know that in fact I don't even need operator=.

What might be the problem? Can someone please jave a look and help me?

Thanks in advance for your time.

4

2 回答 2

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

... is parsed as

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

... because the * binds to node1 and not the type name.

What you wanted to write is either

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

or

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

The latter is obviously superior because it prevents you from doing such mistakes in the future :).

Pointers are independent of the = operator, and you wont need the definition, unless you want to assign raw objects.

于 2013-07-14T22:37:54.150 回答
1

Do you aware that

BSTNode<string,string>* node1,node2;

is equivalent to

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

?

If you do aware, then the correct format for the operator= should probably be

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

If you just want to copy the pointer, all you need to do is:

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