链接器不能识别重载函数,这是为什么呢?

ghosty_hand 2010-01-08 04:17:26
template<typename T> class CPVRTSingleton
{
private:
CPVRTSingleton(const CPVRTSingleton&);
CPVRTSingleton & operator=(const CPVRTSingleton&);

public:
static T& inst()
{
static T object;
return object;
}

static T* ptr()
{
return &inst();
}

protected:
CPVRTSingleton() {};
virtual ~CPVRTSingleton() {};
};


这是一个基类,继承过来,

class TextureManager : public CPVRTSingleton<TextureManager>
{
public:
TextureManager(){};
~TextureManager();//cpp里有实现
TextureManager(int i32TotalTextures);//cpp里有实现
....
}


奇怪的问题出现了,我定义一个TextureManager的对象,会报链接错误,无法识别析构函数。我改为定义一个对象指针,如果有new 操作,也会报这个链接错误,会不识别第二个构造函数和析构函数。如果只是个空指针,则可以通过链接,请问这是为什么呢?在线等你们的回答哦。
...全文
160 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghosty_hand 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 traceless 的回复:]
Faint ~~

你给的代码,我放到main.cpp 文件里编译可以通过
并且使用如下:TextureManager *pt = new TextureManager(8);
[/Quote]

我调用 *pt = new TextureManager(8); 的话,则不能识别第二个构造函数也就是现在调用的这个,和析构函数。
如果调用 *pt = new TextureManager(); 的话 不能识别析构函数。
DLevel 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yshuise 的回复:]
模板不支持分离编译吧。放在同一个文件试试。

[/Quote]

这名词专业,跟我第一次说一个意思
yshuise 2010-01-08
  • 打赏
  • 举报
回复
模板不支持分离编译吧。放在同一个文件试试。
DLevel 2010-01-08
  • 打赏
  • 举报
回复
不好意思,看错了,看这个URL
应该有帮助,如果你重载<<错误的话,这URL里面还有一些其他的templateLNK 2109错误
http://msdn.microsoft.com/zh-cn/library/799kze2z(VS.80).aspx

// LNK2019e.cpp
// compile with: /EHsc
// LNK2019 expected
#include <iostream>
using namespace std;

template<class T> class
Test {
friend ostream& operator<<(ostream&, Test&);
// Uncomment the following line to resolve.
// template<typename T> friend ostream& operator << (ostream&, Test<T>&);
};

template<typename T>
ostream& operator<<(ostream& os, Test<T>& tt) {
return os;
}

int main() {
Test<int> t;
cout << "Test: " << t << endl; // unresolved external
}
traceless 2010-01-08
  • 打赏
  • 举报
回复
Faint ~~

你给的代码,我放到main.cpp 文件里编译可以通过
并且使用如下:TextureManager *pt = new TextureManager(8);
ghosty_hand 2010-01-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dlevel 的回复:]
应该是 模板类的定义只能在头文件里面。
你定义到了CPP里面,出这问题正常
[/Quote]

这个模板类就是定义在h文件里的,这是opengl es2.0 一个开发引擎里的一个模板
xixiaoliu 2010-01-08
  • 打赏
  • 举报
回复
LZ 把 .h 和 .cpp 都贴出来看看吧
内容长的话,把函数里的实现代码去掉
DLevel 2010-01-08
  • 打赏
  • 举报
回复
把,xigou函数实现,放到.H文件里面
还有就是记住上边的原则
ghosty_hand 2010-01-08
  • 打赏
  • 举报
回复
定义的地方

class Myclass
{
TextureManager *m_pTexManager;
void MyFunc();
};

MyFunc()
{
m_pTexManager = new TextureManager(); //加上这句就会报错 或者直接定义成对象也会报错
}
DLevel 2010-01-08
  • 打赏
  • 举报
回复
应该是 模板类的定义只能在头文件里面。
你定义到了CPP里面,出这问题正常
pengzhixi 2010-01-08
  • 打赏
  • 举报
回复
你这个很多函数后面的分号是怎么回事,另外你这个要建立一个工程,然后将所有的文件都丢到里面编译。
ghosty_hand 2010-01-08
  • 打赏
  • 举报
回复
OGLE2Application.obj : error LNK2019: 无法解析的外部符号 "public: virtual __thiscall pvrengine::TextureManager::~TextureManager(void)" (??1TextureManager@pvrengine@@UAE@XZ),该符号在函数 "public: virtual void * __thiscall pvrengine::TextureManager::`scalar deleting destructor'(unsigned int)" (??_GTextureManager@pvrengine@@UAEPAXI@Z) 中被引用
Debug/OGLES2DemoApplication.exe : fatal error LNK1120: 1 个无法解析的外部命令
traceless 2010-01-08
  • 打赏
  • 举报
回复
你的指针是哪个类型指针
ghosty_hand 2010-01-08
  • 打赏
  • 举报
回复
sorry, TextureManager() {} 这里本来就没有分号的,是我刚才贴上去不小心附上的。跟这个应该没有关系
xixiaoliu 2010-01-08
  • 打赏
  • 举报
回复

template<typename T>
class CPVRTSingleton
{
private:
CPVRTSingleton(const CPVRTSingleton&)
{

}

CPVRTSingleton & operator=(const CPVRTSingleton&)
{
return *this;
}

public:
static T& inst()
{
static T object;
return object;
}

static T* ptr()
{
return &inst();
}

protected:
CPVRTSingleton()
{

}

virtual ~CPVRTSingleton()
{

}
};


class TextureManager : public CPVRTSingleton<TextureManager>
{
public:
TextureManager()
{

}

~TextureManager()
{

}

TextureManager(int i32TotalTextures)
{

}

};



int main()
{
TextureManager tm;

return 0;
}


看起来正常
xixiaoliu 2010-01-08
  • 打赏
  • 举报
回复
TextureManager(){};


这个分号去掉

64,681

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧