在c++中编译模板结构
Compile template struct in C++
我使用c++模板开发了一个数组库来处理多种类型{int, float, double}。我将结构体和函数的定义和声明存储在Util.h和Util.cpp文件中。我在arraysample。cpp中调用它。但是,我无法编译该项目。c++说对printArray
的未定义引用错误信息
make all
g++ -O2 -g -Wall -fmessage-length=0 -c -o arraySample.o arraySample.cpp
g++ -O2 -g -Wall -fmessage-length=0 -c -o Util.o Util.cpp
g++ -o arraySample arraySample.o Util.o
arraySample.o: In function `main':
/home/vtvan/Desktop/workspace/arraySample/arraySample.cpp:15: undefined reference to `int printArray<double>(Array<double>)'
/home/vtvan/Desktop/workspace/arraySample/arraySample.cpp:22: undefined reference to `int printArray<int>(Array<int>)'
collect2: ld returned 1 exit status
make: *** [arraySample] Error 1
请帮我解决这个问题,我已经尝试了很多次,但无法解决它。很奇怪,当我将3个文件组合在一起时,它可以很好地工作。所以请给我一些解决方案。我在这里附上3个文件的源代码供您参考。
非常感谢,我期待着你的回复。
Util.h
#ifndef UTIL_H_
#define UTIL_H_
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#define ARRAYMAXNDIMS 3
// multi-dimensional array
template <typename T>
struct Array {
T *x;
int size;
int nDims;
int N[ARRAYMAXNDIMS];
};
template <typename T> int printArray(Array<T> A);
#endif /* UTIL_H_ */
Util.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <typeinfo>
#include "Util.h"
template <typename T> int printArray(Array<T> A) {
int i = 0, j = 0;
int k,l;
std::string typeT = typeid(T).name();
std::string format(" %9f");
if (!typeT.compare("i"))
format = " %d";
printf("Array with dimension (%d",A.N[0]);
k=1;
while(k<A.nDims && k<ARRAYMAXNDIMS){
printf(",%d",A.N[k]);
k++;
}
printf(")n");
switch (A.nDims) {
case 1: // print 1D array
for(k=0;k<A.N[0];k++){ printf(" %9d",A.x[k]);}
printf("n");
return(1);
case 2: // print 2D array
for(k=0;k<A.N[0];k++){
for(l=0;l<A.N[1];l++) {
printf(format.c_str(),A.x[k+l*A.N[0]]);
}
printf("n");
}
return(1);
case 3: // print last 2 dimensions of a 3D array, where the index of the first dimension is i
if(i<0 || i>=A.N[0]) {
printf("index %d is outside the range of indices for the first dimension, i.e. [0:%d]n",i,A.N[0]-1);
return(0);
}
printf("printing slice (%d,:,:)n",i);
for(k=0;k<A.N[1];k++){
// printf("%5d ",k);
for(l=0;l<A.N[2];l++) printf(" %9d",A.x[i+k*A.N[0]+l*A.N[0]*A.N[1]]);
printf("n");
}
return(1);
}
}
arraySample.cpp
#include <stdio.h>
#include <stdlib.h>
#include "Util.h"
int main(void) {
Array<double> a;
a.nDims = 2; a.N[0] = 2; a.N[1] = 2; a.size = 4;
a.x = (double *)calloc(a.size,sizeof(double));
a.x[0] = 1.23; a.x[1] = 2.23; a.x[2] = 3.23; a.x[3] = 5.23;
printArray(a);
Array<int> b;
b.nDims = 2; b.N[0] = 2; b.N[1] = 2; b.size = 4;
b.x = (int *)calloc(b.size,sizeof(int));
b.x[0] = 1; b.x[1] = 2; b.x[2] = 3; b.x[3] = 5;
printArray(b);
return 0;
}
应该在头文件中定义模板函数printArray
。把Util.cpp
移到Util.h
。
这样想。当编译器编译main.cpp
时,它到达printArray(a)
,然后想实例化printArray
模板,将T
作为double
。但是,它看不到函数的定义,因此无法生成相应的代码。因此,为了使函数实现在此时对编译器可见,您应该将其放在头文件中。
另一种实现完全相同功能的方法是将#include <Util.cpp>
放在Util.h
的底部(而不是将#include <Util.h>
放在Util.cpp
的顶部)。然而,给模板实现文件一个.tpp
扩展名是非常常见的做法。
你已经在某种程度上回答了自己的问题。您需要通过剪切和粘贴代码或在util.h文件中使用#include指令来组合util.cpp代码和util.h文件。然后,当您在arraySample.cpp中#包含util.h文件时,您将获得所需的效果。有关这方面的解释和一些历史,请参阅Vandevoorde和Josuttis的"c++模板"第6章。
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 从函数中全局删除并重新实例化数组结构,而无需在编译时知道数组的大小
- C++编译大型结构时内存使用过多
- 如何在 C++11 编译时构建具有递增值的整数成员的结构序列
- C 嵌套结构上的 g++ 编译错误
- 无法在窗口上编译结构 - 缺少 ltdl.h
- 编译时检查以确保结构中的任何位置都没有填充
- Rcpp - 在包结构之外编译
- 当编译时已知引用占用结构中的空间时,是否错过了优化?
- 尝试初始化结构向量时出现编译错误
- ARM C++编译器不会编译具有可变常量成员的结构
- Jetson 工具链文件夹结构和交叉编译时的 libgomp 链接器错误
- std::结构复杂,使编译速度变慢
- 为什么我无法编译结构化绑定?
- 如何修复编译由结构组成的 std::p air 时出现的错误
- NVCC编译了特征库,并在运行时失败的结构中的MatrixxD大小
- 将类似JavaScript的结构编译为静态,快速C++的最佳方法是什么?
- 使用数组和"结构"编译错误?
- 它无法将 c++ 映射结构编译为 map<A、pair<B、C> >
- Boost精灵规则与phoenix绑定到结构编译失败