c++ -为什么体系结构中没有定义的符号:

C++ - Why Undefined symbols for architecture x86_64:

本文关键字:定义 符号 为什么 体系结构 c++      更新时间:2023-10-16

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

头文件:

#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 +中。

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

试着这样开始你的标题

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

类模板中方法的定义应该是

  1. 定义在header
  2. 定义在for . .impl文件中,包含在声明头
  3. 在。cpp文件中显式实例化,在这种情况下,只能使用实例化的版本。
这个问题是因为你在源文件中定义了方法。