我正在学习C++,我在
.h
文件,以及
.cpp
文件这个
.h类
文件如下:
class Matrix {
private:
int r;
int c;
double* d;
public:
Matrix(int nrows, int ncols, double ini = 0.0);
~Matrix();
inline double operator()(int i, int j) const;
inline double& operator()(int i, int j);
};
以及
.cpp公司
是:
#include "matrix.h"
Matrix::Matrix(int nrows, int ncols, double ini) {
r = nrows;
c = ncols;
d = new double[nrows*ncols];
for (int i = 0; i < nrows*ncols; i++) d[i] = ini;
}
Matrix::~Matrix() {
delete[] d;
}
inline double Matrix::operator()(int i, int j) const {
return d[i*c+j];
}
inline double& Matrix::operator()(int i, int j) {
return d[i*c+j];
}
测试文件为:
#include <iostream>
#include "matrix.h"
using namespace std;
int main(int argc, char *argv[]) {
Matrix neo(2,2,1.0);
cout << (neo(0,0) = 2.34) << endl;
return EXIT_SUCCESS;
}
问题:
当我编译
test.cpp
文件使用
g++ test.cpp
g++ test.cpp matrix.cpp
,我得到错误:
warning: inline function 'Matrix::operator()' is not defined
和
ld: symbol(s) not found for architecture x86_64
.
问题:
什么是失败?我怎样才能理解发生了什么?谢谢你的帮助!