6

This is not a duplicate. I've checked lots of answers, the FAQ and else. Nothing of that told me news. Here is the simplified code. It's the minimum to get and explain the error.

/*** Polynomial.hpp ********************************************************/

namespace Modulus
{
// Forward declaration of the types and non-inline template friend functions.

template <typename T>
    class Polynomial;

template <typename T>
    Polynomial<T>  operator +
        (Polynomial<T> const & p,
         Polynomial<T> const & q);
}

namespace Modulus
{

template <typename T>
class Polynomial
{
public:
    Polynomial() { }

    // [!] when you comment this in, you get the error.
    //Polynomial      operator +      () const { return *this; }

    friend Polynomial operator + <> (Polynomial const & p,
                                     Polynomial const & q);
};

} // namespace

// Template: include .cpp file.
   //#include "Polynomial.cpp"
///^ It is commented out, for compiling in one file.


/*** Polynomial.cpp ********************************************************/

namespace Modulus
{

template <typename T>
Polynomial<T>
    operator + (Polynomial<T> const & p,
                Polynomial<T> const & q)
{
    return Polynomial<T>();
}

} // namespace


/*** main.cpp **************************************************************/


//#include "Polynomial.hpp"

using namespace Modulus;

int main()
{
    Polynomial<int> p;
    p + p;
    return 0;
}

When I comment the line under [!] in, the error I get is friends can only be classes or functions (Clang++) or declaration of ‘operator+’ as non-function (g++).

For me, it seems the compilers mistake the two operators. As far as I've learned the operator overloading stuff, the unary and binary operators are completely independent and can be uniquely distinguished by their number of arguments.

So why does the error occur? Making the unary operator a friend using the standard practice, makes the code compile fine on both compilers.

1
  • 2
    Interesting. If you put the unary operator+ below the friend declaration of the binary one, both gcc and clang compile it without complaint, if you put it above, you get a compiler error.
    – Wintermute
    Mar 28, 2015 at 14:05

1 Answer 1

4

When you declare something in a scope, it hides delcarations of the same name in any wider scope. Here, the declaration of the member operator+ hides that of the non-member. So the friend declaration refers to the member, not the non-member, hence the error.

You'll need to qualify the name if you want to refer to both in the same scope:

Polynomial      operator +      () const { return *this; }

friend Polynomial Modulus::operator + <> (Polynomial const & p, Polynomial const & q);
                  ^^^^^^^^^
5
  • 1
    It appears that the OP's real header includes the source file at the bottom.
    – chris
    Mar 28, 2015 at 14:10
  • 1
    @chris: So it does. That's nasty. Mar 28, 2015 at 14:10
  • It is common enough, but typically, the extension would be something other than cpp. Doesn't libstdc++ use tpp?
    – chris
    Mar 28, 2015 at 14:11
  • 3
    libstdc++ uses .tcc. Some editors even recognize that as C++ source file extension (for syntax highlighting, indentation etc.), so it's not a bad way to follow if you want to do things this way.
    – Wintermute
    Mar 28, 2015 at 14:15
  • 1
    Indeed, "nasty" refers to the choice of name (confusing both for my poor old brain, and for automated tools and scripts), not the concept of hiding definitions in a separate include file. Mar 28, 2015 at 14:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.