0

I am trying to get this to work but it comes up with an error message

C2473 " operator << looks like a function definition but there is no parameter list."

Basically, this is a header file and I want to declare a namespace and a template for a class also overriding the << operator.

Please help! (I'm also new to stackoverflow and this is my first question) :)

#ifndef header
#define header

namespace nmsp{
  template <class T> 
  class expt; //forward declaration of class, so friend function can be defined as a template
  template <class T> 
  std::ostream& operator << (std::ostream& os, const expt <T> &input);


template <class T> 
class expt {


    friend std::ostream& operator << (std::ostream& os, const expt <T>& input);

private: 
    T *data;
public: 
    expt() { data = null; }

};

template <class T> 
std::ostream& nmsp:: operator << (std::ostream& os, const expt <class T>& input) {
    //....
}
}
}
#endif
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
AliS
  • 3
  • 3

1 Answers1

1

Here's your fixed code (see the comments):

namespace nmsp{
  template <class T> 
  class expt; // forward declaration of class, so friend function can be 
              // defined as a template
  template <class T> 
  std::ostream& operator << (std::ostream& os, const expt <T> &input);


template <class T> 
class expt {

    // templated friend declarations don't inherit the outer 
    // class template parameters, you need to make them a template separately
    template <class U>
    friend std::ostream& operator << (std::ostream& os, const expt <U>& input);

private: 
    T *data;
public: 
    expt() { data = nullptr; }
                 // ^^^^^^^ null isn't a thing

};

}

// Move the definition out of the namespace
template <class T> 
std::ostream& nmsp:: operator << (std::ostream& os, const expt <T>& input) {
                                                            // ^^^ drop class
    //....
    return os;
}
// Remove the extra }

Live Demo

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Hi thank you for the super quick response, but I'm still getting a bunch of errors. I dropped the class in my definition of the operator << but the error message is : "nmsp::expt": "T" is not a valid template type argument for parameter T.... – AliS Apr 26 '16 at 16:27
  • @AliS Did you really take the code from my demo as a starting point? The most important thing is the first redeclaration of the `friend` function in `expt`. – πάντα ῥεῖ Apr 26 '16 at 16:29
  • I'm sorry I didn't know what you mean I was just trying to get my code working and am quite stressful. Could you possibly explain a bit more about what you meant in your comment "templated friend declarations don't inherit the outer class template parameters, you need to make them a template separately" ? Because in my original code, it seems to be having a problem the first time I mentioned operator. (the word 'operator' did not light up as the others...) – AliS Apr 26 '16 at 16:38
  • @AliS Well, may be [that warning](http://coliru.stacked-crooked.com/a/a04cafe57fbdfe1f) is a bit clearer about the problem, I can't tell how your IDE and indexer are working. Don't give that much about syntax highlighting and indexer hints during typing code. Concentrate on the warnings and errors when you compile your code actually. – πάντα ῥεῖ Apr 26 '16 at 16:45