-2

我对 Visual Studio 2012 和 2015 有疑问,因为它看起来比“ifndef”不起作用。我对“NAN”使用“ifndef”,对头文件使用“ifndef”,据说这两个错误(见图)。当我在其他文件的标题中添加链接“#include”Outil.h“”时,我看到了相同的错误消息。

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

我也尝试更改第一个函数“realoc_ungraded”的名称,但它不起作用,我得到同样的错误。

错误信息

消息:

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

2) 错误:LNK2005: "struct tab_dynamo __cdecl realoc_ugraded(struct tab_dynamo,unsigned int)" (?realoc_ugraded@@YA?AUtab_dynamo@@U1@I@Z) 已在 main.obj Project1 中定义

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

有不同文件的代码:

文件 main.cpp

#include"Outil.h"

int main(void) {

    return 0;
}

文件输出.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

文件 Outil.cpp:

#include"Outil.h"
4

1 回答 1

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

当预处理器处理这些时,NAN 已定义,因为它尚未定义。

#include<cmath>

然后 cmath 可能包含 math.h,发现 NAN 是由你定义的。

您可以尝试更改包含的顺序和您的定义。

#include <cmath>

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

顺便说一句,如果您使用 gcc 编译,您可以使用 -E 选项查看预处理器的输出并了解预处理器如何扩展宏。

于 2017-03-06T02:57:33.037 回答