C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了C:避免重复的符号链接器错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_801_1@
我收到链接错误

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: duplicate symbol)我不认为我在多个地方定义了read()函数,但链接器(clang)说我做了.

我在哪里复制read()符号?

我的@L_262_9@结构如下所示:

//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

最后是驱动@L_262_9@:

// 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() { ... }

大佬总结

以上是大佬教程为你收集整理的C:避免重复的符号链接器错误全部内容,希望文章能够帮你解决C:避免重复的符号链接器错误所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。