我正在寻求您的帮助,因为我正在使用模板,而且似乎我误解了一些东西。
这是我的代码:
arbre.h
template<typename T>
class Arbre;
template<typename T>
class Noeud
{
friend class Arbre<T>;
public :
Noeud();
~Noeud();
T get_info()const;
private :
T info;
Noeud<T> *fg, *fd;
};
template<typename T>
class Arbre
{
public :
//Constructors-------------------------------------------------------------
Arbre();
/*
other function definitions
*/
}
arbre.tpp
#include "arbre.h"
template <typename T>
Noeud<T>::Noeud(){
fg = fd = 0;
}
template <typename T>
Noeud<T>::~Noeud(){
}
template <typename T>
T Noeud<T>::get_info()const{
return info;
}
template <typename T>
Arbre<T>::Arbre(){
racine = 0;
}
/*
other function implementations...
*/
main.cpp包含“ arbre.h”并创建一个像这样的对象:
Arbre<int> a;
所以按照这篇关于显式实例化的文章:http ://www.cplusplus.com/forum/articles/14272/
我在 arbre.tpp 的末尾添加了这两行:
template class Noeud<int>;
template class Arbre<int>;
这样链接器就可以在 arbre.o 中为我的对象的 int 版本编译代码,这样我就可以将实现 (arbre.tpp) 与标头 (arbre.h) 分开。我的代码使用隐式实例化(arbre.tpp 包含在 header.h 的末尾),但我想将它们分开。
我想知道为什么链接失败:
g++ -o main.o -c main.cpp -Wall -g -std=c++11
g++ -o arbre.o -c arbre.tpp -Wall -g -std=c++11
g++: warning: arbre.tpp: linker input file unused because linking not done
g++ -o arbre.out main.o arbre.o
g++: error: arbre.o: No such file or directory
Makefile:9: recipe for target 'arbre.out' failed
make: *** [arbre.out] Error 1
你有什么主意吗 ?我正在做与我链接的文章中相同的事情,不是吗?
提前致谢。