我知道将字符串文字作为模板参数传递的唯一方法是在之前声明它:
档案啊
#ifndef A_H
#define A_H
#include <string>
char EL[] = "el";
template<char* name>
struct myclass
{
std::string get_name() { return name; }
};
typedef myclass<EL> myclass_el;
#endif
文件 a.cpp
#include "a.cpp"
主文件
#include "a.h"
...
g++ -c a.cpp
g++ -c main.cpp
g++ -o main main.o a.o
我得到了:
a.o:(.data+0x0): multiple definition of `EL'
main.o:(.data+0x0): first defined here
collect2: ld returned 1 exit status
我不能声明EL
为外部,我想保留a.cpp
. 解决方案?