2016-03-05 99 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> 
相关问题