C++ C+中的错误LNK2005+;如果没有';行不通

C++ C+中的错误LNK2005+;如果没有';行不通,c++,lnk2005,C++,Lnk2005,我对VisualStudio2012和2015有一个问题,那就是“ifndef”似乎不起作用。我用“ifndef”表示“NAN”,用“ifndef”表示头文件,上面说了这两个错误(参见图)。当我在其他文件的头中添加链接“#include“Outil.h”时,我看到了相同的错误消息 我以前总是这样做,而且总是工作。我不明白为什么它现在即使只有两个文件也不能工作 我还试图更改第一个函数“realoc_ungraded”的名称,但它不起作用,我得到了相同的错误 信息: 1) 警告:C4005:“NA

我对VisualStudio2012和2015有一个问题,那就是“ifndef”似乎不起作用。我用“ifndef”表示“NAN”,用“ifndef”表示头文件,上面说了这两个错误(参见图)。当我在其他文件的头中添加链接“#include“Outil.h”时,我看到了相同的错误消息

我以前总是这样做,而且总是工作。我不明白为什么它现在即使只有两个文件也不能工作

我还试图更改第一个函数“realoc_ungraded”的名称,但它不起作用,我得到了相同的错误

信息:

1) 警告:C4005:“NAN”:math.h的宏重新定义

2) 错误:LNK2005:“结构选项卡\u dynamo\u cdecl realoc\u ugraded(结构选项卡\u dynamo,unsigned int)”(?realoc\u ugraded@@YA?AUtab\u dynamo@@U1@I@Z) 已在main.obj Project1中定义

3) 错误:LNK1169:找到一个或多个多重定义符号

有不同文件的代码:

文件main.cpp

#include"Outil.h"

int main(void) {

    return 0;
}
文件Outil.h

#ifndef LIBRARY_OF_TOOLS
#define LIBRARY_OF_TOOLS 0

#define _USE_MATH_DEFINES

//NAN not defined in Visual Studio 2012, so I use the def. of VS 2015
#ifndef NAN
#define NAN ((float)(std::numeric_limits<float>::infinity*0.0F))
#endif

#include<iostream>
#include<string>
using namespace std;

#include<cmath>
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>

#define ERROR_ADRESSE   0xcccccccc //default when not initialised
#define DEFAULT_LENGHT_TAB  1

//-----------------------------------------

typedef double type_data; //the type for calculation

//-----------------------------------------

/*Struct for my array*/
typedef struct {
    type_data *tab;
    unsigned int length;
}tab_dynamo;

//-----------------------------------------

template<typename T>
bool verify_ptr(const T *ptr) {
    return (ptr == NULL || ptr == (T*)(ERROR_ADRESSE));
}

//-----------------------------------------

template<typename T>
void see_tab(const T *tab, const unsigned int taille) {
    unsigned int i;
    cout << endl << endl;
    if (verify_ptr(tab) == false && taille > 0) {
        cout << endl;
        for (i = 0; i<taille; ++i) {
            cout << tab[i] << "\t";
        }
    }
    cout << endl << endl;
}

//-----------------------------------------

template<typename T>
T* realoc_ungraded(const T* original_tab, unsigned int *length, const unsigned int new_length) {
    T* new_tab = NULL;
    unsigned int precedent_length = 0, i;

    /*1) Exception case to directly exit*/
    if (new_length == 0) {
        return NULL;
    }

    /*2) Verification of the ptr of the length*/
    if (verify_ptr(length)) {
        length = (unsigned int*)calloc(1, sizeof(unsigned int));
        assert(length);
    }

    precedent_length = *length;
    *length = new_length;

    /*4) Creation of the new tab.*/
    new_tab = (T*)calloc(*length, sizeof(T));
    assert(new_tab);

    /*5) To transfert data of the original tab to the new tab*/
    if (precedent_length != 0 && verify_ptr(original_tab) == false) {
        for (i = 0; i < precedent_length && i < new_length; ++i) {
            new_tab[i] = original_tab[i];
        }
    }
    return new_tab;
}

//-----------------------------------------

//Version with the use of the struct "tab_dynamo"
tab_dynamo realoc_ungraded(tab_dynamo original_tab, const unsigned int new_length) {
    tab_dynamo tableau = { NULL, 0 };
    tableau.tab = realoc_ugraded(original_tab.tab, &original_tab.length, new_length);
    tableau.length = new_length;
    return tableau;
}



#endif
#ifndef NAN
#定义NAN((浮点)(标准::数值限制::无穷大*0.0F))
#恩迪夫
当预处理器处理这些时,会定义NAN,因为它尚未定义

#include<cmath>
#包括
然后cmath可能包括math.h,发现NAN是由您定义的

您可以尝试更改include的顺序和定义

#include <cmath>

#ifndef NAN
#define NAN ((float)(std::numeric_limits<float>::infinity*0.0F))
#endif
#包括
#伊芬德夫南
#定义NAN((浮点)(标准::数值限制::无穷大*0.0F))
#恩迪夫

B.T.W如果使用gcc编译,可以使用-E选项查看预处理器的输出,并了解预处理器如何扩展宏。

也很有用。是的,我可以使用此选项,但我选择使用与Visual Studio 2015中相同的定义,以确保它在Visual Studio 2012和2015中的工作方式完全相同。但这并不能解释为什么在已经定义了“NAN”的情况下“ifndef”不受尊重。将错误消息作为文本包含在问题中,而不是图像。不要在标题中定义函数。声明它们,然后在一个位置定义。可能重复
#include<cmath>
#include <cmath>

#ifndef NAN
#define NAN ((float)(std::numeric_limits<float>::infinity*0.0F))
#endif