C ++ - 为什么体系结构x86_64的未定义符号:

时间:2014-05-12 11:22:35

标签: c++ templates

我是C ++的新手,我尝试在C ++中学习template,所以我从 Essential C ++ 编写了一个Matrix模板类。

这是头文件:

#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

这是源文件:

#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;
}

当我在Sublime中编译它时,它显示:

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)

似乎问题发生在函数operator +

那么,为什么会发生这种情况,我该如何解决?

2 个答案:

答案 0 :(得分:0)

类模板中方法的定义应为

  1. 在标题 OR
  2. 中定义
  3. 定义为ex。 .impl文件并包含在声明标题 OR
  4. 在.cpp文件中明确实例化,在这种情况下,只能使用实例化的版本。
  5. 问题发生的原因是您在源文件中定义方法。

答案 1 :(得分:0)

尝试像这样开始你的标题

//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>& );

并在类中声明好友函数

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