S*******s 发帖数: 13043 | 1 in my project, I have following 3 files
//t.h
#include
template
myFunc (std::vector& var){
....
}
//a.cpp
#include "t.h"
void funcA(){
std::vector a;
std::vector b;
myFunc(a);
myFunc(b);
}
void funcB();
int main(){
funcA();
funcB();
}
//b.cpp
#include "t.h"
void funcB(){
std::vector a;
std::vector b;
myFunc(a);
myFunc(b);
}
if I build it, I'll get an error that myFunc(std::vector&) defined
twice.
if I manage them in di |
q*****g 发帖数: 72 | 2 myFunc included twice
put #ifndefine at t.h
【在 S*******s 的大作中提到】 : in my project, I have following 3 files : //t.h : #include : template : myFunc (std::vector& var){ : .... : } : //a.cpp : #include "t.h" : void funcA(){
|
t****t 发帖数: 6806 | 3 好象不是这个原因,OP用的什么编译器?
理论上说,template function可以定义多次的
【在 q*****g 的大作中提到】 : myFunc included twice : put #ifndefine at t.h
|
S*******s 发帖数: 13043 | 4 hehe, not related to compilor ba.
when a.cpp is compiled, myFunc(vector) is generated in a.obj
when b.cpp is compiled, myFunc(vector) is generated in b.obj
when these two obj get linked, same function definition colided.
【在 t****t 的大作中提到】 : 好象不是这个原因,OP用的什么编译器? : 理论上说,template function可以定义多次的
|
S*******s 发帖数: 13043 | 5 then, seems a static will do? let me try try.
【在 S*******s 的大作中提到】 : hehe, not related to compilor ba. : when a.cpp is compiled, myFunc(vector) is generated in a.obj : when b.cpp is compiled, myFunc(vector) is generated in b.obj : when these two obj get linked, same function definition colided.
|
t****t 发帖数: 6806 | 6 我说了,template 可以被定义多次的。不需要one definition rule.
【在 S*******s 的大作中提到】 : hehe, not related to compilor ba. : when a.cpp is compiled, myFunc(vector) is generated in a.obj : when b.cpp is compiled, myFunc(vector) is generated in b.obj : when these two obj get linked, same function definition colided.
|