我收到链接器错误:
duplicate symbol __ZN5ENDF64FileILNS_7MF_enumE1EE4readEv in:
Read.cpp.o
Material.cpp.o
其中重复的符号名称是:
$ c++filt __ZN5ENDF64FileILNS_7MF_enumE1EE4readEv
ENDF6::File<(ENDF6::MF_enum)1>::read()
我知道我无法在多个位置定义相同的函数,这就是导致此链接器错误的原因。 (我看过这个问题:ld:重复符号 https://stackoverflow.com/questions/282700/ld-duplicate-symbol)我认为我没有read()
函数在多个地方定义,但是链接器(clang++
)说我愿意。
我在哪里复制read()
symbol?
我的代码结构如下所示:
//MFs.hpp
#ifndef MFS_HPP
#define MFS_HPP
enum class MF_enum {
...
}
#endif
//File.hpp
#ifndef FILE_HPP
#define FILE_HPP
#include "MFs.hpp"
// Definition of class File
template<>
class File {
...
}
// Definition of File<...>::read() function
template <>
void File<1>::read()
{
std::cout << "Reading into MF=1"<< std::endl;
}
#endif
没有File.cpp
因为File
类是模板化的。所有定义(和声明)都在File.hpp
// Material.cpp
#include "File.hpp"
...
// Material.hpp
#ifndef MATERIAL_HPP
#define MATERIAL_HPP
#include "File.hpp"
...
#endif
最后是驱动代码:
// Read.cpp
#include "Material.hpp"
#include "File.hpp"
int main (){
...
}
模板的(完整)特化并不是模板本身。如果您正在专门化该函数,那么您需要在标头中声明它并在单个翻译单元中提供实现,或者使定义内联:
// Header [1]
template <int>
class File {
// ...
void open();
};
template <>
void File<1>::open(); // just declaration
// Single .cpp
template <>
void File<1>::open() { ... }
或者:
// Header [2]
template <int>
class File {
// ...
void open();
};
template <>
inline void File<1>::open() { ... }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)