c++使用模板 new时提示undefined reference

风一样走了 2016-05-18 10:41:02
poor.h代码:
#ifndef _POOR_H_
#define _POOR_H_

using namespace std;

//池
template<class D>
class Poor
{
public:
Poor();

~Poor();
};

#endif


poor.cpp

#include "poor.h"

template<class D>
Poor<D>::Poor()
{
}

template<class D>
Poor<D>::~Poor()
{
}



typedef struct
{
int key;
char value[20];
} stParseMap;

int main (int argc,char *argv[])
{
Poor<stParseMap> *b = new Poor<stParseMap>();//该行编译报错
return 0;
}


makefile:
#!/bin/ksh
IFLAGS=-DSS_64BIT_SERVER -D_REENTRANT -D_DSCI_ -D_THREAD_LOG_ -D_DEBUG_INFO_ -D_TIME_LOG_
INCLUDE=-I.
LIB=-L.
.SUFFIXES:.cpp .o

CFLAGS = ${IFLAGS} ${INCLUDE}

CC=g++ -m64 -g -O2 -fpermissive

OBJ = ibeartest.o poor.o

BIN=ibeartest

all:${BIN} install clean

.cpp.o:
echo "Compiling the $<"
$(CC) -c ${CFLAGS} $<
.c.o:
echo "Compiling the $<"
$(CC) -c ${CFLAGS} $<

clean:
rm -f ibeartest.o poor.o

######################################################################

${BIN}: $(OBJ)
$(CC) ${CFLAGS} -o $@ $? ${LIB}

######################################################################

install:

程序在hp-hx acc上编译可以通过~
移植到linux g++ 编译时报错:
make -f makefile
g++ -m64 -g -O2 -fpermissive -DSS_64BIT_SERVER -D_REENTRANT -D_DSCI_ -D_THREAD_LOG_ -D_DEBUG_INFO_ -D_TIME_LOG_ -I. -o ibeartest ibeartest.o poor.o -L.
ibeartest.o: In function `main':
/ocsapp/account/work/wangzhia/cc/ibeartest.cpp:24: undefined reference to `Poor<stParseMap>::Poor()'
collect2: ld 返回 1
make: *** [ibeartest] 错误 1
...全文
256 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
风一样走了 2016-05-18
  • 打赏
  • 举报
回复
引用 3 楼 lianshaohua 的回复:
模板不要写在两个文件中
非常感谢,学习了!!!
风一样走了 2016-05-18
  • 打赏
  • 举报
回复
引用 2 楼 lianshaohua 的回复:
#include "poor.cpp"
可以了,谢谢大神。 是因为使用了模板的原因么?
ztenv 版主 2016-05-18
  • 打赏
  • 举报
回复
模板不要写在两个文件中
风一样走了 2016-05-18
  • 打赏
  • 举报
回复
求大神指教,别沉啊!!!
ztenv 版主 2016-05-18
  • 打赏
  • 举报
回复
#include "poor.cpp"

64,675

社区成员

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

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