g++和模板的链接器错误
Linker error with g++ and templates
我正在做一个简单的程序,当我找不到编译它的方法时,我正在使用C++中的模板来实现一个带有一些数据类型的库。
代码为:
VectorT.cpp文件:
template <class T>
VectorT<T>::VectorT(){
elementos = NULL;
numElementos=0;
}
VectorT.h文件:
#ifndef __VectorT_h__
#define __VectorT_h__
template <class T>
class VectorT{
private:
T * elementos;
int numElementos;
public:
VectorT();
};
#include "VectorT.tpp"
#endif
以及编译方式,带有错误:
g++ -c -o obj/VectorT.o src/VectorT.cpp -I include/VectorT.h
g++: warning: src/VectorT.cpp: linker input file unused because linking not done
知道吗?非常感谢。
这个"代码"无法编译,因为它仍然不是一个完整的代码。
通过这种方式,您可以编写测试代码,并且只需要添加头文件。例如:g++-o bin/testVector src/testVector.cpp-I include/而且这个程序不用编译任何其他东西就可以工作!唯一的细节是头文件和cpp文件必须在/include文件夹中,"VectorT.tpp"和VectorT.h,头文件和模板的实现。
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误