2016-03-05 23 views
-2

我正在製作一個用於學習目的的C++頭文件。它應該給我的數字的長度(是一個INT,浮動,雙等),而不包括逗號。這裏是我的代碼:C++ #include宏名稱必須是標識符

#ifndef NUMLEN_H 
#define NUMLEN_H 
#define <cstring> 

int numlen (char numberLengthSample[]) //numlen - lenghth of the number, numberLengthSample - the number you put in. 
{ 
    int numberLengthDummy = strlen (numberLengthSample); //a temporary int used in counting the length 
int numlenc = strlen (numberLengthSample); //the true length of the number 
while (numberLengthDummy>0) 
{ 
    if (numberLengthSample[numberLengthDummy-1]=='.'||numberLengthSample[numberLengthDummy-1]==',') 
    { 
     numlenc--; 
    } 
    numberLengthDummy--; 
} 
return numlenc; 
} 

#endif // NUMLEN_H 

但它給了我3個錯誤: 1)(3號線)宏名必須是標識符 2)(7號線) 'strlen的' 不是在這個範圍內(顯然申報) 3)(第5行(當從我的測試.cpp執行時))初始化'int numlen(char *)'的參數1 [-fpermissive] |

我試圖尋找答案,但沒有佔上風。任何幫助,將不勝感激 :) 。

回答

0

在C++中,你必須#include頭,而不是#define他們:

#ifndef NUMLEN_H 
#define NUMLEN_H 
#include <cstring> 

這不是與該問題相關,但只應在頭文件中聲明函數並在源文件內實現它們。

0

的問題是因爲你使用的#define代替的#include,它應該是:

#include <cstring> 
相關問題