error: declaration of ‘operator*’ as non-function

1.9k views Asked by At

I have some problems with bound template friend functions of template classes. I've refered the page which has the similar and simple problem as me: vect.hpp:13:33: error: declaration of ‘operator<<’ as non-function , but I still confused, the problem cannot be solved. Here are my codes:

#include<iostream>  
#include<valarray>

using namespace std;

//forward declaration
template<typename ElemType, size_t row, size_t col = row>
class matrix;

template<typename ET, size_t R, size_t C>
matrix<ET, R, C> operator*(ET multier, matrix<ET, R, C> mattem);

template<typename ElemType, size_t row, size_t col>
class matrix
{
    private:
        valarray<ElemType> mat;
    public:
        matrix(): mat(row*col) {}
        matrix(initializer_list<ElemType>);

        matrix operator*(ElemType multier) const
        {
            matrix mattem;
            mattem.mat = this->mat*multier;
            return mattem;
        }
        
        //friend function prototype
        friend matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem);

        void show() const
        {
            for (ElemType Elem : this->mat)
                cout << Elem << " ";
            cout << endl;
        }
};

template<typename ElemType, size_t row, size_t col>
matrix<ElemType, row, col>::matrix(initializer_list<ElemType> ini)
{
    try
    {
        if (ini.size() != row*col)
            throw "Inappropriate length!";
        this->mat = ini;
    }
    catch (const char * str)
    {
        cout << str << endl;
    }
}

//friend function definition
template<typename ElemType, size_t row, size_t col>
matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem)
{
    return mattem*multier;
}

int main(void)
{
    matrix<int, 2, 3> test = {1, 3, 5, 8, 9, 7};
    matrix<int, 2, 3> abc =2*test;
    abc.show();
}

I compiled and run it in WSL2 (Ubuntu 9.3.0-17ubuntu1~20.04) using gcc version 9.3.0, and I got a series errors as below:

test.cpp:28:51: error: declaration of ‘operator*’ as non-function
   28 |         friend matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem);
      |                                                   ^
test.cpp:28:51: error: expected ‘;’ at end of member declaration
   28 |         friend matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem);
      |                                                   ^
      |                                                    ;
test.cpp:28:52: error: expected unqualified-id before ‘<’ token
   28 |         friend matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem);
      |                                                    ^
test.cpp:54:91: error: template-id ‘operator*<>’ in declaration of primary template
   54 | matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem)
      |                                                                                           ^
test.cpp: In function ‘int main()’:
test.cpp:62:29: error: conversion from ‘matrix<[...],[...],2>’ to non-scalar type ‘matrix<[...],[...],3>’ requested
   62 |     matrix<int, 2, 3> abc =2*test;
      |                            ~^~~~~
2

There are 2 answers

2
Serge Ballesta On BEST ANSWER

I found that the most robust way to deal with friend operators in template classes was to put the full definition in the class itself:

//forward declaration
template<typename ElemType, size_t row, size_t col = row>
class matrix;
/*
template<typename ET, size_t R, size_t C>
matrix<ET, R, C> operator*(ET multier, matrix<ET, R, C> mattem);
*/
template<typename ElemType, size_t row, size_t col>
class matrix
...
        //friend function prototype
        friend matrix<ElemType, row, col> operator *  (ElemType multier, matrix<ElemType, row, col> mattem){
            return mattem * multier;
        }
...
/*
//friend function definition
template<typename ElemType, size_t row, size_t col>
matrix<ElemType, row, col> operator*(ElemType multier, matrix<ElemType, row, col> mattem)
{
    return mattem*multier;
}
*/
0
Caleth On

You don't need <> in that declaration. Annoyingly the first error isn't telling you that, but something else as a result of the compiler not knowing what you meant.

You can simplify the declaration to

friend matrix operator*(ElemType multier, matrix mattem);

The definition still requires the full template parameter list

template<typename ElemType, size_t row, size_t col>
matrix<ElemType, row, col> operator*(ElemType multier, matrix<ElemType, row, col> mattem)
{
    return mattem*multier;
}