[问题] (C++)关于template separation compilation model

楼主: xie29 (xie29)   2017-04-30 15:48:11
开发平台(Platform): (Ex: Win10, Linux, ...)
macos
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
xcode
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
问题(Question):
各位大大好,小弟为c++的初学者,现在正在看c++ primer(3rd),在template编译模组时碰到了一点问题
,我的目的是想要将template的宣告和定义分开,但编译一直给我linker error的讯息,有去google了但
原因我还是看不太懂(抱歉我太菜了...),但有试过他们提供的解决方法都有效,但还是想知道为什么无法
执行..感谢!
喂入的资料(Input):
预期的正确结果(Expected Output):
错误结果(Wrong Output):
Undefined symbols for architecture x86_64:
"int mmm<int>(int, int)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
程式码(Code):(请善用置底文网页, 记得排版)
//File:Min.hpp
#ifndef _MIN
#define _MIN
template <class Type>
extern Type _min(Type , Type);
#endif
楼主: xie29 (xie29)   2017-04-30 16:13:00
* template编译模型 抱歉打错了
作者: cyanis (Terry)   2017-04-30 16:34:00
我猜是因为忘了角括号<>?<int>啊~刚刚查了应该不用,请无视上面的
楼主: xie29 (xie29)   2017-04-30 17:47:00
好的 还是感谢大大回复更正:在错误讯息那边的mmm就是_min,因为ㄧ开始以为是function名称有冲突所以随便改ㄧ个名字
作者: jerryh001   2017-04-30 18:47:00
应该不能这样分开吧?
作者: grayStone (灰色石头)   2017-04-30 19:01:00
你要让template在compile time知道型态compiler分别编了min.o main.o但是link的时候才发现需要Type=int的function
楼主: xie29 (xie29)   2017-04-30 21:31:00
J大:不行吗? 因为我看书上这样教,我就实作看看了不过的确compiler不给我过..g大:感谢您的解说!非常清楚!谢谢~
作者: grayStone (灰色石头)   2017-05-01 01:51:00
可以分只是要写在min.cpp告诉编译器要做int的或是include implement.hpp不过都写template了自己是觉得分到cpp意义不大
楼主: xie29 (xie29)   2017-05-01 12:59:00
g大:那时候是想说,以后会不想公开实作档,所以才想说试试,但看了网络上solution后感觉有点此地无银三百两..所以最后就觉得还是宣告跟定义写在ㄧ起好了!! 感谢大大的回复!很有帮助
作者: hunandy14 (Charlott.HonG)   2017-05-01 13:00:00
楼主: xie29 (xie29)   2017-05-01 21:57:00
感谢h大的回复~

Links booklink

Contact Us: admin [ a t ] ucptt.com