Search code examples
c++operatorsoperator-overloadingno-match

no match for 'operator=' in


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.


Solution

  • 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.