C++ lnk2017这个错误是因为什么?

找了好久没找到具体什么原因,大家看看一下错误到底在哪,错误提示和代码如下:

img


#include<iostream>
#include<vector>
#include<fstream>
 
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 rows, int columns);
    Matrix(const Matrix&);
    ~Matrix();
    Matrix& operator+(const Matrix&);
 
    void operator+=(const Matrix&);
    elemType& operator()(int row, int column)
    {
        return _matrix[row*cols() + column];
    }
 
    const elemType& operator()(int row,int column)const
    {
        return _matrix[row*cols() + column];
    }
 
    int rows()const { return _rows; }
    int cols() const { return _cols; }
 
    bool same_size(const Matrix &m) const 
    {
        return rows == m.rows() && cols == m.cols();
    }
 
    bool comfortable(const Matrix &m)const { return (cols() == m.rows()); }
    ostream& print(ostream&)const;
 
protected:
    int _rows;
    int _cols;
    elemType *_matrix;
};
template <typename elemType>
inline ostream&
operator<<(ostream& os,const Matrix<elemType> &m)
{
    return m.print(os);
}
 
//Matrix.h文件结束
 
 
//深度拷贝 deep copy 
template <typename elemType>
Matrix<elemType>::Matrix(const Matrix &rhs)
{
    _rows = rhs._rows; _cols = rhs._cols;
    int mat_size = _rows * _cols;
    _matrix = new elemType[mat_size];
    for (int ix = 0; ix < mat_size; ++ix)
        _matrix[ix] = rhs._matrix[ix];
}
 
template <typename elemType>
Matrix<elemType>& Matrix<elemType>::operator+(const Matrix &rhs)
{
    if (this != &rhs) {
        _rows = rhs._rows; _cols = rhs._cols;
        int mat_size = _rows*_cols;
        delete[] _matrix;
        _matrix = new elemType[mat_size];
        for (int ix = 0; ix < mat_size; ++ix) {
            _matrix[ix] = rhs._matrix[ix];
        }
    }
    return *this;
}
 
template<typename elemType>
Matrix<elemType>
operator+(const Matrix<elemType> &m1, const Matrix<elemType> &m2)
{
    //确定m1和m2大小相同
    Matrix<elemType> result(m1);
    result += m2;
    return result;
}
 
template <typename elemType>
Matrix<elemType>
operator*(const Matrix<elemType> &m1, const Matrix<elemType> &m2) {
    //确定m1的行数(row)等于m2的列数(cloumn)
    Matrix<elemType> result(m1.rows(), m2.cols());
    for (int ix = 0; ix < m1.rows(); ix++) {
        for (int jx = 0; jx < m2.cols(); jx++)
        {
            result(ix, jx) = 0;
            for (int kx = 0; kx < m1.cols(); kx++)
                result(ix, jx) += m1(ix, kx)*m2(kx, jx);
        }
    }
    return result;
}
 
template <typename elemType>
void Matrix<elemType>::operator+=(const Matrix &m) {
    //确定m1和m2大小相同
    int matrix_size = cols()*rows();
    for (int ix = 0; ix < matrix_size; ++ix) {
        (*(_matrix + ix)) += (*(m._matrix + ix));
    }
}
 
template <typename elemType>
ostream& Matrix<elemType>::print(ostream &os)const {
    int col = cols();
    int matrix_size = col*rows();
    for (int ix = 0; ix < matrix_size; ++ix) {
        if (ix%col == 0)os << endl;
        os << (*(_matrix + ix)) << ' ';
    }
    os << endl;
    return os;
}
 
int main() 
{
    ofstream log("log.txt");
    if (!log) { cerr << "can't open log file!\n"; return 0; }
 
    Matrix<float> identity(4, 4);
    log << "identity: " << identity << endl;
    float ar[16] = { 1., 0., 0., 0., 0., 1., 0., 0.,
    0., 0., 1., 0., 0., 0., 0., 1. };
 
    for (int i = 0, k = 0; i < 4; ++i)
        for (int j = 0; j < 4; ++j)
            identity(i, j) = ar[k++];
    log << " identity after set: " << identity << endl;
 
    Matrix<float> m(identity);
    log << "m: memberwise initialized: " << m << endl;
 
    Matrix<float> m2(8, 12);
    log << "m2: 8x12: " << m2 <<endl;
 
    m2 = m;
    log << "m2 after memberwise assigned to m: " << m2 << endl;
 
    float ar2[16] = { 1.3, 0.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 };
 
    Matrix<float> m3(4, 4);
    for (int ix = 0, kx = 0; ix < 4; ++ix)
        for (int j = 0; j < 4; ++j)
            m3(ix, j) = ar2[kx++];
 
    log << "m3:assigned random values: " << m3 << endl;
    Matrix<float> m4 = m3*identity; log << m4 << endl;
    Matrix<float> m5 = m3 + m4; log << m5 << endl;
 
    m3 += m4; log << m3 << endl;
    return 0;
}


```

18-20行3个函数有声明,没定义。

【以下回答由 GPT 生成】

根据提供的代码和问题描述,我们可以推测 lnk2017 错误是由于以下原因之一导致的:

  1. 函数定义与函数声明不一致。
  2. 模板类成员函数的定义不在头文件中。

解决方案:

首先,我们需要确保函数定义与函数声明一致。在提供的代码中,operator+operator+= 函数的定义与声明不一致。在类的声明中,这两个函数被声明为成员函数,但在实现中却以全局函数的形式进行定义。需要将这两个函数的定义从全局函数转换为类成员函数。

然后,我们需要确保模板类成员函数的定义在头文件中。在提供的代码中,模板类的成员函数定义没有在头文件中,这可能导致编译器无法正确实例化模板类的成员函数,从而导致 lnk2017 错误。

修改后的代码如下所示:

#include <iostream>
#include <vector>
#include <fstream>
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 rows, int columns);
    Matrix(const Matrix&);
    ~Matrix();
    Matrix& operator+(const Matrix&);
    void operator+=(const Matrix&);

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

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

    int rows() const { return _rows; }
    int cols() const { return _cols; }

    bool same_size(const Matrix &m) const
    {
        return rows() == m.rows() && cols() == m.cols();
    }

private:
    // 添加成员变量
    int _rows;
    int _cols;
    vector<elemType> _matrix;
};

template <typename elemType>
Matrix<elemType> operator+(const Matrix<elemType>& m1, const Matrix<elemType>& m2)
{
    // implementation...
}

template <typename elemType>
Matrix<elemType> operator*(const Matrix<elemType>& m1, const Matrix<elemType>& m2)
{
    // implementation...
}

template <typename elemType>
Matrix<elemType>::Matrix(int rows, int columns) : _rows(rows), _cols(columns), _matrix(rows * columns)
{
    // implementation...
}

template <typename elemType>
Matrix<elemType>::Matrix(const Matrix& other) : _rows(other._rows), _cols(other._cols), _matrix(other._matrix)
{
    // implementation...
}

template <typename elemType>
Matrix<elemType>::~Matrix()
{
    // implementation...
}

template <typename elemType>
Matrix<elemType>& Matrix<elemType>::operator+(const Matrix& other)
{
    // implementation...
}

template <typename elemType>
void Matrix<elemType>::operator+=(const Matrix& other)
{
    // implementation...
}

通过将 operator+operator+= 函数定义为类的成员函数,并将所有成员函数的定义和声明放入头文件中,我们可以解决 lnk2017 错误。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^