0

I am new to C++, and I try learn template in C++, so I wrote a Matrix template class from Essential C++.

Here is header file:

#ifndef __MATRIX__
#define __MATRIX__  

#include <iostream> 

using namespace std;    

template <typename elemType>
class Matrix 
{
    friend Matrix<elemType> operator + ( const Matrix<elemType>&, const Matrix<elemType>& );
    friend Matrix<elemType> operator * ( const Matrix<elemType>&, const Matrix<elemType>& );
public:

    Matrix( int row, int column );
    Matrix( const Matrix& );    

    ~Matrix() { delete [] _matrix; }    

    int row() const { return _row; }
    int column() const { return _column; }  

    Matrix& operator = ( const Matrix& );   

    ostream& print( ostream& ) const;
    void operator += ( const Matrix& ); 

    elemType& operator () (int row, int column ) 
    {
        return _matrix[row * _row + column]; 
    }   

    elemType operator () (int row, int column ) const 
    { 
        return _matrix[row * _row + column]; 
    }   

private:    
    elemType *_matrix;
    int _row;
    int _column;
};  

#endif

Here is source file:

#include "Matrix.h" 

template <typename elemType>
Matrix<elemType>::Matrix( int row, int column ) :
    _row(row), _column(column)
{
    int size = _row * _column;
    _matrix = new elemType[size];
    for (int i = 0; i < size; ++i)
    {
        _matrix[i] = elemType();
    }
}   

template <typename elemType>
Matrix<elemType>::Matrix( const Matrix& m )
{
    cout << "Matrix( const Matrix& m )" << endl;
    _row = m._row;
    _column = m._column;
    int size = _row * _column;
    _matrix = new elemType[size];
    for (int i = 0; i < size; ++i)
    {
        _matrix[i] = m._matrix[i];
    }
}   

template <typename elemType>
Matrix<elemType>& Matrix<elemType>:: operator = ( const Matrix& m )
{
    cout << "operator = " << endl;
    if( this != &m )
    {
        _row = m._row;
        _column = m._column;
        int size = _row * _column;
        delete [] _matrix;
        _matrix = new elemType[size];
        for (int i = 0; i < size; ++i)
        {
            _matrix[i] = m._matrix[i];
        }
    }
    return *this;
}   

template <typename elemType>
ostream& Matrix<elemType>::print( ostream& os ) const
{
    os << "===print the matrix:===" << endl;
    for (int i = 0; i < _row; ++i)
    {
        for (int j = 0; j < _column; ++j)
        {
            os << _matrix[i * _row + j] << ' ';
        }
        os << endl;
    }
    return os;
}   

template <typename elemType>
void Matrix<elemType>::operator += ( const Matrix& m )
{
    for (int i = 0; i < m.row(); ++i)
    {
        for (int j = 0; j < m.column(); ++j)
        {
            _matrix[i * _row + j] += m(i,j);
        }
    }
}   

template <typename elemType>
Matrix<elemType> operator + ( const Matrix<elemType>& a, const Matrix<elemType>& b )
{
    Matrix<elemType> res(a);
    res += b;
    return res;
}   

template <typename elemType>
Matrix<elemType> operator * ( const Matrix<elemType>& a, const Matrix<elemType>& b )
{
    Matrix<elemType> res( a.row(), b.column() );
    for (int i = 0; i < a.row(); ++i)
    {
        for (int j = 0; j < b.column(); ++j)
        {
            res(i, j) = 0;
            for (int k = 0; k < a.column(); ++k)
            {
                res(i, j) += a(i, k) * b(k, j);
            }
        }
    }
    return res;
}   

template <typename elemType>
inline ostream& operator << ( ostream& os, const Matrix<elemType>& m )
{
    return m.print(os);
}   

int main(int argc, char const *argv[])
{   

    Matrix<int> ima (4,4);
    cout << ima << endl;    

    Matrix<float> fma (4,4);
    cout << fma << endl;    

    float ar[16] = 
    {
        1., 0., 0., 0., 0., 1., 0., 0.,
        0., 0., 1., 0., 0., 0., 0., 1.
    };  

    Matrix<float> fmb(4,4);
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            fmb(i,j) = ar[i*4+j];
        }
    }   

    fma = fmb;  

    cout << fma << fmb << endl; 

    float br[16] = { 1.3, .4, 2.6, 8.2, 6.2, 1.7, 1.3, 8.3,
                    4.2, 7.4, 2.7, 1.9, 6.3, 8.1, 5.6, 6.6 };
    for (int i = 0; i < 4; ++i)
    {
        for (int j = 0; j < 4; ++j)
        {
            fmb(i,j) = br[i*4+j];
        }
    }           

    cout << fma << fmb << endl;     

    fma += fma; 

    cout<< fma << fmb << endl;  

    fma = fma + fma;    

    cout << fma << fmb << endl; 

    return 0;
}

When I complier it in Sublime, it shows:

Undefined symbols for architecture x86_64:
  "operator+(Matrix<float> const&, Matrix<float> const&)", referenced from:
      _main in Matrix-d2d621.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It seems the problem happened in function operator +.

So, why it happened and how can I solve it?

Tim
  • 2,121
  • 2
  • 20
  • 30
  • You never instantiate the template. – David Schwartz May 12 '14 at 11:26
  • 1
    Templates have to be declared in *.h files only. There are some workarounds, but you can make this work by moving all your *.cpp code to the *.h file. Check out http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Sharadh May 12 '14 at 11:26
  • Would you be so kind to tell us what compiler (including version) and operating system you use? I saw a similar error from OS X 10.9. – usr1234567 May 12 '14 at 12:14
  • @user2799037 gcc 4.2.1 OSX10.9.2 – Tim May 12 '14 at 12:52
  • @Tim Maybe this is your real issue? http://stackoverflow.com/q/16352833 – usr1234567 May 12 '14 at 13:11

2 Answers2

1

Try to start your header like this

//declare class and friend operators templates
template <typename elemType>
class Matrix;

template <typename elemType>
Matrix<elemType> operator + ( const Matrix<elemType>&, const Matrix<elemType>& );
template <typename elemType>
Matrix<elemType> operator * ( const Matrix<elemType>&, const Matrix<elemType>& );

And declare friend functions in class like this

friend Matrix<elemType> operator + <> ( const Matrix<elemType>&, const Matrix<elemType>& );
friend Matrix<elemType> operator * <> ( const Matrix<elemType>&, const Matrix<elemType>& );
Ation
  • 731
  • 7
  • 16
0

The definitions of methods in a class Template should be

  1. Defined in header OR
  2. Defined in for ex. .impl file and included in the declaring header OR
  3. Explicitly instantiated in a .cpp file, in that case only the instantiated versions can be used.

The problem happens because you are defining the methods in a source file.

Rakib
  • 7,435
  • 7
  • 29
  • 45