1

novice C++ programmer here.

I'm using Numerical Recipes (V3) source code as part of a larger, modulated C++ project.

While I'll try not to get into the specifics of my problem, I am curious as to why these NR header files do not incorporate any header guards? I'm aware this question is very specific to those who have used this code in the past, but looking at the NR forums they seem quite inactive...

The errors I'm finding with my code that prompted this question are;

error LNK2005: "void __cdecl gaussj(class NRmatrix &)" (?gaussj@@YAXAAV?$NRmatrix@N@@@Z) already defined in Schmidt_V2_(Zeta).obj

error LNK2005: "void __cdecl gaussj(class NRmatrix &,class NRmatrix &)" (?gaussj@@YAXAAV?$NRmatrix@N@@0@Z) already defined in Schmidt_V2_(Zeta).obj

4

1 回答 1

2

AFAIK没有充分的理由。在某些情况下,您可能不希望使用包含防护(请参阅此问题),但这不是其中之一。

如果您需要在项目的多个位置包含这些标头,则必须引入自己的守卫,如下所示:

#include <a_normal_thing>
#include <another_normal_thing>

#ifndef SPECIAL_NONESENSE_H
#define SPECIAL_NONESENSE_H
#include <special_nonsense>
#endif

// More normal includes...

这既冗长又烦人,但它会起作用。

编辑:或者现在,#pragma once正如唐尼在下面的评论中建议的那样使用它是非常安全的。这在 3.4 之前的 GCC 版本中不起作用,但您可能不再需要支持它。

于 2016-02-18T16:48:49.110 回答