views:

137

answers:

5

I am trying to implement a List class using pointers and am trying to implement a function LOCATE(T x) where T is for the template and returns the first position of the element x if found, else returns last position + 1.

My functions code is

template<class T>
 int List<T>::locate(T n) const
 {
  int size = end();
  Node<T> * p = head_;

  for (int i = 0; i < size; i++)
  {
   if (p->data() == n) // fails on this line
    return i;
   p = p->link();
  }
  return size; // if no match found
     }

I initialise my list with T as string as

List<string> myList;

but I get an error message

'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'std::string

Why is the error coming up even though the '==' operator is defined for the string class? '

The code for Node is

template<typename T>
class Node
{
  public:

 // Constructors
 Node();
 Node(T d, Node<T> * l = NULL);

 //Inspectors 
 T data() const;
 Node<T> * link() const;

 // Mutators 
 void data(T d); // assigns new value to Node
 void link(Node<T> * l); // points this Node to a different one

 // Destructor
 ~Node();

    private:
  Node<T> * link_;
  T data_;
};

template<typename T>
 T Node<T>::data() const
 {
  return data_;
 }
template<typename T>
 Node<T>* Node<T>::link() const
 {
  return link_;
 }

The calling code is

List<string> test;
test.add("abc");
cout << test.locate("abc") << endl;
A: 

The reference to std::istreambuf_iterator is peculiar as nothing in the code you show justifies it -- can you please show us Node and whatever other code impinges on this in a minimal failing example? Trying to evince the problem from very partial code and an error message is very much like pulling teeth...!-)

Alex Martelli
A: 

This looks OK, I can't see how the std::istreambuf_iterator gets into the picture...

One thing you may want to adjust is taking const T& instead of T as in-parameters to your methods, e.g.

  Node(const T& d, Node<T> * l = NULL);
  void data(const T& d);

  int List<T>::locate(const T& n) const { ...

What with the actual problem, there's bound to be something else going on.

Kim Gräsman
+1  A: 

Try :

if( n.compare(p->data()) == 0 )

string::compare documentation

As the comments below have noted, operator== should work. Please double check that you have

#include <string>
using std::string;
Jesse
This seems to be working. Thanks
Jaelebi
That's all well and good... but you really didn't solve the problem. operator== should work.
rlbond
@rlbond most definitely. I know some documentation for string (http://www.cplusplus.com/reference/string/string/) does not include operator==, but visual c++ does (http://msdn.microsoft.com/en-us/library/8ww0haah.aspx)
Jesse
It's in the standard, section 21.2.
rlbond
wow. I missed the include<string> in the main file. I guess its too late in the night (2 AM here) to continue writin code. The == works after the include statement.
Jaelebi
+4  A: 

Without getting neck-deep in your code, I notice several problems.

Firstly,

locate(T n)

should be

locate(const T& n)

This saves a possible copy of n

And to ask a stupid question, are you sure you've done:

 #include <string>
Rodyland
Good call on asking about the include
Jesse
Well done..according to latest comment by OP, the include file turns out to be the problem.
Naveen
Thanks! :)Don't you just love obscure C++ compiler errors?
Rodyland
A: 

Start deleting code untill it works again. Some typo or rogue macros or conflicting namespaces screw things up.

Will this compile by itself?

string data = "bla";
Node<string> p("bla");
bool b = p.data() == data;

(Every C++ programmer should make a cout << "bla" << end; typo. Very entertaining)

Eugene