0

So I'm still some what new to C++ programming and very new at templates. I am trying to make a basic template class (a node if you will) that holds some generic data and a double. I then want to make another class contain a set of the previously mentioned template class.

Im having trouble with the less-than operator as its going to server as my comparator.

Node&Tree.h

#ifndef _POINTNODE_H_
#define _POINTNODE_

#include <set>

template<typename T>
class PointNode {

 public:

  PointNode(double p){ m_point = p;}
  ~PointNode();

  bool operator < (const &PointNode<T> p1) const;

 private:

   const double m_point;
   T *m_data;

};

template <typename T>
class PointTree {

 public:

  PointTree();
  ~PointTree();

 private:

  std::set<PointNode<T> > tree;

};

#endif

Node&Tree.cpp

#inlcude "Node&Tree.h"
#include <set>

template<typename T>
bool PointNode<T>:: operator < (const &PointNode<T> p1) const{
   return m_point < p1.m_point;
}

Im getting the folowing errors

Node&Tree.cpp:5:39: error: ISO C++ forbids declaration of ‘parameter’ with no type [-   fpermissive]
Node&Tree.cpp:5:39: error: expected ‘,’ or ‘...’
Node&Tree.cpp:5:6: error: prototype for ‘bool PointNode<T>::operator<(const int&)   const’ does not match any in class ‘PointNode<T>’
Node&Tree.h:15:8: error: candidate is: bool PointNode<T>::operator<(const int&)"

This is largely unimplemented but I just wanted to get the basics to compile at least... And any pointers on the code or if you think I'm going about this all wrong please tell me!

Any help would be amazing!

taocp
  • 23,276
  • 10
  • 49
  • 62
Odub
  • 33
  • 2
  • 9
  • Along with the solution provided below, pls make sure the definition of template class methods are visible to the compiler.Have a look at http://stackoverflow.com/questions/5180357/why-do-c-template-definitions-need-to-be-in-the-header – Arun Apr 19 '13 at 03:04

2 Answers2

2
bool PointNode<T>:: operator < (const &PointNode<T> p1) const

should be:

 bool PointNode<T>:: operator < (const PointNode<T>& p1) const

You put the reference & in the wrong position, so you have that forbids declaration of parameter error. Another place has the same error.

bool operator < (const &PointNode<T> p1) const;

should be

bool operator < (const PointNode<T>& p1) const;
taocp
  • 23,276
  • 10
  • 49
  • 62
0

Make PointNode object as reference

bool operator < (const PointNode<T>& p1) const;

And its defination

template<typename T>
bool PointNode<T>:: operator < (const PointNode<T>& p1) const{
   return m_point < p1.m_point;
}

this will fix the problem.

shivakumar
  • 3,297
  • 19
  • 28