C重复符号

(苹果电脑)

我尝试了名称空间,包括警卫,编译指示一次等.

基本上,这是结构:

的CMakeLists.txt

add_executable(Game Game/main.cpp Game/rtexture.cpp)

游戏/ main.cpp中

#include "cleanup.h"

//...
cleanup(foobar);

游戏/ rtexture.cpp

#include "cleanup.h"

//...
cleanup(foobar);

cleanup.h

//varIoUs includes

template<typename T,typename... Args>
void cleanup(T *t,Args&&... args){
    //Cleanup the first item in the list
    cleanup(t);
    //Recurse to clean up the remaining arguments
    cleanup(std::forward<Args>(args)...);
}
/*
 * These specializations serve to free the passed argument and also provide the
 * base cases for the recursive call above,eg. when args is only a single item
 * one of the specializations below will be called by
 * Cleanup(std::forward<Args>(args)...),ending the recursion
 * We also make it safe to pass nullptrs to handle situations where we
 * don't want to bother finding out which values Failed to load (and thus are null)
 * but rather just want to clean everything up and let cleanup sort it out
 */
template<>
void cleanup<SDL_Window>(SDL_Window *win){
    if (!win){
        return;
    }
    SDL_DestroyWindow(win);
}
template<>
void cleanup<SDL_Renderer>(SDL_Renderer *ren){
    if (!ren){ 
        return;
    }
    SDL_DestroyRenderer(ren);
}
template<>
void cleanup<SDL_Texture>(SDL_Texture *tex){
    if (!tex){
        return;
    }
    SDL_DestroyTexture(tex);
}
template<>
void cleanup<SDL_Surface>(SDL_Surface *surf){
    if (!surf){
        return;
    }
    SDL_FreeSurface(surf);
}

如果有人问,我确实从教程中获取了这个“cleanup.h”,但是却找不到将它包含在多个类中的方法,而没有声明重复的符号.

Home at cruz45488-y19-MBA13-12 in ~/desktop/sdlworkspace/tmp
$make
Linking CXX executable Game
duplicate symbol __ZN5RUtil7cleanupI10SDL_WindowJEEEvPT_DpOT0_ in:
    CMakeFiles/Game.dir/Game/main.cpp.o
    CMakeFiles/Game.dir/Game/rtexture.cpp.o
duplicate symbol __ZN5RUtil7cleanupI12SDL_RendererJEEEvPT_DpOT0_ in:
    CMakeFiles/Game.dir/Game/main.cpp.o
    CMakeFiles/Game.dir/Game/rtexture.cpp.o
duplicate symbol __ZN5RUtil7cleanupI11SDL_TextureJEEEvPT_DpOT0_ in:
    CMakeFiles/Game.dir/Game/main.cpp.o
    CMakeFiles/Game.dir/Game/rtexture.cpp.o
duplicate symbol __ZN5RUtil7cleanupI11SDL_SurfaceJEEEvPT_DpOT0_ in:
    CMakeFiles/Game.dir/Game/main.cpp.o
    CMakeFiles/Game.dir/Game/rtexture.cpp.o

有帮助吗?谢谢.

解决方法

显式函数模板特化取决于一个定义规则,就像常规函数一样.添加内联以允许标头中的定义;或者在源文件中定义它们,并在标题添加声明.

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...