1
votes

I made this code

Declaration:

template <class T>
class Matrix
{
    std::vector< std::vector<T> > mat;
    size_t rows , cols;
public:

    Matrix<T>();
    Matrix<T>(const std::string);
    Matrix(const size_t r, const size_t c, const std::vector<T> v);
    Matrix<T> operator=(const Matrix<T> &other);

    friend std::ostream& operator<<(std::ostream &os , Matrix<T> m);
};

Functions:

template <class T>
std::ostream& operator<<(std::ostream &os , Matrix<T> m){

    for (size_t i = 0; i < m.rows; i++)
    {
        for (size_t j = 0; j < m.cols; j++)
        {
            os << m.mat[i][j] << " ";
        }
        os << std::endl;
    }
    return os;
}

Main :

int main(){
    std::vector<int> v(9);
    v[0] = 1;
    v[1] = 2;
    v[2] = 3;
    v[3] = 4;
    v[4] = 5;
    v[5] = 6;
    v[6] = 7;
    v[7] = 8;
    v[8] = 9;
    Matrix<int> m = Matrix<int>(2, 3, v);
    std::cout << m;
}

I get this error :

Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream

&,class Matrix)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@V?$Matrix@H@@@Z) referenced in function _main C:\Users\igor\documents\visual studio 2013\Projects\matrix\matrix\Source.obj matrix

I tried to write it without the friend but got a different error.
What am I doing wrong ?

1
Is the definition in a .h file that is #included in main.cpp? Posting an MCVE will be very useful. - R Sahu
I don't know if that as any thing to do with your problem (I don't think it does) but you should still have youre function take a const Matrix<T>& instead of a Matrix<T> - Amxx
@IgorGumush define the templated operator directly in the header file, not in a .cpp file - vsoftco
Since this is a template, the definition of operator<< must be #included with the header. Did you do this? - 5gon12eder

1 Answers

3
votes

You must implement a template in the header-file, not only declare it there.
Unless, of course, you can explicitly instantiate all the needed specializations.

Anyway, consider defining your friend inline:

template <class T>
class Matrix
{
    //...
    friend std::ostream& operator<<(std::ostream &os , Matrix<T> m) {
        // do things
        return os;
    }
};

You won't be able to call it explicitly unless you also declare it in the containing scope, but ADL will find it, and that's all that you want anyway.