使用.cpp使用G 在同一目录上使用.CPP,错误
Compiling .h with .cpp using g++ on the same directory, error
我正在学习C ,并且在.h
文件中有类声明,并且在.cpp
文件上的定义。.h
文件如下:
// matrix.h
class Matrix {
private:
int r; // number of rows
int c; // number of columns
double* d;
public:
Matrix(int nrows, int ncols, double ini = 0.0); // declaration of the constructor
~Matrix(); // declaration of the destructor
inline double operator()(int i, int j) const;
inline double& operator()(int i, int j);
};
和 .cpp
是:
// matrix.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];
}
测试文件是:
// test.cpp
#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;
}
问题:当我使用g++ test.cpp
编译test.cpp
文件或使用g++ test.cpp matrix.cpp
时,我会得到错误:warning: inline function 'Matrix::operator()' is not defined
和ld: symbol(s) not found for architecture x86_64
。
问题:什么是失败?我怎么能理解发生了什么?感谢您的帮助!
inline
函数的主体应在该函数的所有呼叫中可见。
在您的设置中,您需要将这两个内联定义从matrix.cpp
移动到matrix.h
;或使它们成为非内线。
相关文章:
- 使用cpp中的结构和函数的多个学生条目
- 在 swift 项目中使用 CPP 库
- 使用 cpp-httplib 的 POST 请求
- 如何使用 CPP REST SDK 实现 CAS(中央认证服务)?
- 如何使用cpp编写选择排序算法以降序对元素列表进行排序?
- 尝试使用 CPP 构建类时"unresolved external..."
- 使用.cpp文件让 RcppArmadillo 在 Rstudio 中工作
- 如何将 JSON 键值(我使用 cpp boost 解析)存储在相关数据结构中?
- 尝试使用 CPP 在一个 Qt 窗口中显示多个 png 文件
- 如何在 unordered_map 中使用 cpp 来识别 lambda 函数?
- 如何使用 cpp 设置随机数生成器的语法?
- 如何使用 cpp 消除 2d 数组中的边框元素
- CMake add_executable() 使用.cpp文件或 .hpp 文件
- 如何使用 cpp 将 z3 表达式转换为字符串
- 如何使用 cpp 文件系统在目录中查找最新文件
- 在Android应用中使用CPP文件
- 在拖放 QML 列表中插入/删除项目使用 cpp 模型查看
- 在Java项目中使用CPP和OpenCV项目的.so文件
- 使用.cpp使用G 在同一目录上使用.CPP,错误
- 使用CPP将日志发送到Syslog服务器