c ++ #include宏名称必须是标识符

时间:2016-03-05 16:42:12

标签: c++ header

我正在制作一个用于学习目的的C ++头文件。它应该给我数字的长度(无论是 int,float,double 等)而不计算逗号。这是我的代码:

#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 *)'[-fpermissive] |

的参数1

我试图寻找答案,但没有优势。任何帮助将不胜感激:)。

2 个答案:

答案 0 :(得分:0)

问题是因为您使用#define而不是#include,它应该是:

#include <cstring>

答案 1 :(得分:0)

在C ++中,您必须使用#include标题而不是#define标题:

#ifndef NUMLEN_H
#define NUMLEN_H
#include <cstring>

它与问题无关,但您应该只在头文件中声明函数并在源文件中实现它们。

相关问题