
GeneralTemplate.h
#ifndef _GENERATEMPLATE_H_#define _GENERATEMPLATE_H_#include <iostream>template <class T>class GeneralTemplate{ public: GeneralTemplate(); GeneralTemplate(const GeneralTemplate &g); ~GeneralTemplate(); GeneralTemplate& operator= (GeneralTemplate const& g); template <class M> voID arbitraryFunction(const M &m);};#endif main.cpp中
#include "GeneralTemplate.h"#include <iostream>int main(){ GeneralTemplate<int> gInt; gInt.arbitraryFunction(2.3); return 0;} 请注意,我没有任何类模板的成员函数的实现.但那不是问题.我知道怎么做!如果我尝试编译main.cpp,我应该得到一个链接错误,这就是我得到的.问题是为什么它试图找到两次析构函数(下面的两行错误).
$g++ main.cpp /tmp/cckrdPCs.o: In function `main':main.cpp:(.text+0x13): undefined reference to `GeneralTemplate<int>::GeneralTemplate()'main.cpp:(.text+0x34): undefined reference to `voID GeneralTemplate<int>::arbitraryFunction<double>(double const&)'main.cpp:(.text+0x45): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()'main.cpp:(.text+0x61): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()'collect2: ld returned 1 exit status解决方法 它很可能与异常安全有关.如果anyFunction抛出异常,则需要展开堆栈,这意味着需要尽早销毁gInt.由于这一切都发生在main,没有进一步的展开将会发生,它是否真的需要两次调用析构函数是值得怀疑的……但是你所观察到的行为并非完全不在墙上. 总结
以上是内存溢出为你收集整理的c – 为什么要尝试两次找到析构函数?全部内容,希望文章能够帮你解决c – 为什么要尝试两次找到析构函数?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)