2017-03-06 134 views
-2

我有一個問題與Visual Studio 2012 & 2015有關的事實比它似乎比「ifndef」不工作。我使用「ifndef」作爲頭部文件的「NAN」和「ifndef」,它說這兩個錯誤(請參閱圖像)。當我在其他文件的頭文件中添加鏈接「#include」Outil.h「」時,我看到相同的錯誤消息。錯誤LNK2005在C + +和ifndef不工作

我總是這樣做,它總是工作。我不明白爲什麼即使只有兩個文件現在也不起作用。

我也試圖改變第一函數的名稱「realoc_ungraded」,但它不工作,我得到了同樣的錯誤。

Message of error

消息:

1)警告:C4005: 'NAN':math.h中的宏重定義

2)錯誤:LNK2005:「結構tab_dynamo __cdecl realoc_ugraded(結構tab_dynamo ,無符號整型)」(realoc_ugraded @@ YA AUtab_dynamo @@ @ U1 I @ Z)在main.obj PROJECT1已經定義

3)錯誤:?LNK1169:一個或一個以上乘法定義的符號發現PROJET

有不同文件的代碼:

文件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 

文件Outil.cpp:

#include"Outil.h" 
+0

http://en.cppreference.com/w/cpp/types/numeric_limits/quiet_NaN是也有幫助。 –

+0

是的,我可以用這一點,但我選擇使用比在Visual Studio 2015年相同的定義,以確保比它的工作方式完全相同的Visual Studio 2012和2015年。但是,這並不能解釋爲什麼「IFNDEF」是如果已經定義了「NAN」,則不受尊重。 –

+0

將錯誤消息作爲文本包含在您的問題中,而不是圖像。 – 1201ProgramAlarm

回答

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 

B.T.W如果編譯使用gcc,你可以使用-E選項來查看預處理器的輸出,並知道如何預處理擴大宏。

+0

謝謝你的幫助,它現在正常工作。 –