C++ #include 宏名称必须是标识符
c++ #include macro names must be identifiers
我正在制作一个C++头文件用于学习目的。它应该给我数字的长度(无论是整数、浮点数、双精度数等),而不计算逗号。这是我的代码:
#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]|
我试图寻找答案,但没有占上风。任何帮助将不胜感激:)。
在C++中,您必须#include
标头而不是#define
它们:
#ifndef NUMLEN_H
#define NUMLEN_H
#include <cstring>
它与问题无关,但您应该只在头文件中声明函数并在源文件中实现它们。
问题是因为你使用的是 #define 而不是 #include,它应该是:
#include <cstring>
相关文章:
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 既然存在危险,为什么项目要使用-I include开关
- 有充分的理由在h文件中使用include保护而不是cpp文件吗
- 如何将更多文件夹添加到c++include路径
- 什么是"#include <boost/functional/hash.hpp> "?
- #ifdef和未声明的标识符
- f2、f3、f4标识符未找到
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 设置 Visual Studio for MPI: 找不到标识符错误
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- C++包含来自 #include "DevEngine/Core.h" 的错误
- <filesystem> 在 clang 6 和 10 上 #include 错误
- 如何让 GCC/Clang 在保留标识符上出错
- 在 void 函数中使用 #include 变量C++
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- C++ #include 宏名称必须是标识符
- #include <string> 但仍未找到标识符
- mErrCount:虽然在项目属性的include路径中声明了标识符,但未找到该标识符