体系结构x86_64 c++的未定义符号
Undefined symbols for architecture x86_64 c++
我正试图在C++中创建一个自定义的"Matrix"类,但遇到了一个我无法理解的"架构x86_64的未定义符号:"错误。我的相关代码是-
标题
#ifndef CORE_H
#define CORE_H
#include <stdint.h>
class Size
{
public:
Size(const uint64_t nrows, const uint64_t ncols, const uint64_t nframes);
uint64_t rows;
uint64_t cols;
uint64_t frames;
};
template <typename T>
class Matrix
{
public:
Matrix(const uint64_t rows, const uint64_t cols, const uint64_t frames);
Matrix(const uint64_t rows, const uint64_t cols, const uint64_t frames, T *data);
void print();
private:
Size sz;
T *_data;
};
#endif //CORE_H
来源
#include <string.h>
#include <cstdlib>
#include <stdio.h>
#include "core.h"
Size::Size(const uint64_t nrows, const uint64_t ncols, const uint64_t nframes)
{
rows = nrows;
cols = ncols;
frames = nframes;
}
template <typename T>
Matrix<T>::Matrix(const uint64_t rows, const uint64_t cols, const uint64_t frames)
{
Matrix<T>(rows, cols, frames, malloc(rows * cols * frames * sizeof(T)));
}
template <typename T>
Matrix<T>::Matrix(const uint64_t rows, const uint64_t cols, const uint64_t frames, T *data)
{
sz = Size(rows, cols, frames);
_data = data;
}
template <typename T>
void Matrix<T>::print()
{
printf("[");
for (uint64_t f = 0; f < sz.frames; f++) {
printf("[");
for (uint64_t r = 0; r < sz.rows; r++) {
for (uint64_t c = 0; c < sz.cols; c++) {
printf("%.3f,", element(r, c, f));
}
printf("n");
}
printf("]n");
}
printf("]n");
}
测试
#include "core.h"
int main(int argc, char *argv[])
{
int data[] = { 1, 2, 3, 4, 5, 6, 7, 8};
Matrix<int> mat(2, 2, 2, data);
mat.print();
return 0;
}
错误
Undefined symbols for architecture x86_64:
"Matrix<int>::print()", referenced from:
_main in rand.cpp.o
"Matrix<int>::Matrix(int, int, int, int*)", referenced from:
_main in rand.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [examples/rand] Error 1
make[1]: *** [examples/CMakeFiles/rand.dir/all] Error 2
make: *** [all] Error 2
我确信这是件小事,但我想不通。任何帮助都将不胜感激!
模板函数(包括模板类的成员函数)需要在头文件中实现,这样它们的定义(而不仅仅是声明)在所有可能使用它们的翻译单元中都可见。有关更多信息,请参阅此SO问题。
将构造函数和print
函数的定义移动到头文件中,位于Matrix
类模板的定义下方。
相关文章:
- vscode g++链路故障:体系结构x86_64的未定义符号
- 体系结构x86_64的未定义符号:std:terminate(),typeinfo,运算符delete[],运算符new
- C++ 中 dlsym 的未定义符号
- 仅在 MacOS 上析构函数的未定义符号
- OSX clang++:用于 cpp 文件中显式实例化模板的体系结构x86_64的未定义符号
- PCL 出错:体系结构x86_64 @pcl的未定义符号
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 体系结构x86_64的未定义符号:链接器错误
- C++ std::vector<int> 体系结构的未定义符号 x86_64:
- 生成文件:体系结构x86_64的未定义符号
- libc++abi未定义符号:_ZTVN10__cxxabif120__si-class_type_infoE
- Xcode"体系结构x86_64的未定义符号"
- 体系结构x86_64的未定义符号:找不到原因
- SWIG链接器:未定义符号:_ZN2cv8fastFreeEPv(cv::fastFree(void*))
- 如何修复架构x86_64的未定义符号,Boost Asio 1.58
- 在Qt5上去静态库:架构的未定义符号x86_64:
- 体系结构x86_64的未定义符号:c++/ h 文件和 cpp 文件
- 监视 SDK 下的未定义符号"__Unwind_SjLj_Unregister"
- 使用 Boost/Python 的未定义符号 - 复杂
- 建筑x86_64的未定义符号:"vtkDebugLeaksManager::vtkDebugLeaksManager()",引用自: