error: ‘atoi’ was not declared in this scope

本文介绍了一个常见的编程错误——在使用atoi函数时遇到的“atoi未在当前作用域声明”的问题,并提供了解决方案:通过包含标准库头文件stdlib.h来修复此错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出现错误

error: ‘atoi’ was not declared in this scope


解决方法添加头文件

#include<stdlib.h>


PS C:\Users\34171\Desktop\code\WJ> g++ -o WJ Recipe.c main.c main.c: In function 'void read_string(char*, int)': main.c:15:21: error: 'strchr' was not declared in this scope 15 | char *newline = strchr(buffer, '\n'); | ^~~~~~ main.c:4:1: note: 'strchr' is defined in header '<cstring>'; did you forget to '#include <cstring>'? 3 | #include <locale.h> +++ |+#include <cstring> 4 | main.c: In function 'int main()': main.c:102:21: error: 'strlen' was not declared in this scope 102 | if (strlen(updated.name) == 0) { | ^~~~~~ main.c:102:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:103:21: error: 'strcpy' was not declared in this scope 103 | strcpy(updated.name, existing->name); | ^~~~~~ main.c:103:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:108:21: error: 'strlen' was not declared in this scope 108 | if (strlen(updated.category) == 0) { | ^~~~~~ main.c:108:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:109:21: error: 'strcpy' was not declared in this scope 109 | strcpy(updated.category, existing->category); | ^~~~~~ main.c:109:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:114:21: error: 'strlen' was not declared in this scope 114 | if (strlen(updated.ingredients) == 0) { | ^~~~~~ main.c:114:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:115:21: error: 'strcpy' was not declared in this scope 115 | strcpy(updated.ingredients, existing->ingredients); | ^~~~~~ main.c:115:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:121:21: error: 'strlen' was not declared in this scope 121 | if (strlen(input) > 0) { | ^~~~~~ main.c:121:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:122:41: error: 'atoi' was not declared in this scope 122 | updated.prep_time = atoi(input); | ^~~~ main.c:127:21: error: 'strlen' was not declared in this scope 127 | if (strlen(input) > 0) { | ^~~~~~ main.c:127:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:128:41: error: 'atoi' was not declared in this scope 128 | updated.cook_time = atoi(input); | ^~~~ main.c:133:21: error: 'strlen' was not declared in this scope 133 | if (strlen(updated.steps) == 0) { | ^~~~~~ main.c:133:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:134:21: error: 'strcpy' was not declared in this scope 134 | strcpy(updated.steps, existing->steps); | ^~~~~~ main.c:134:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? PS C:\Users\34171\Desktop\code\WJ>
最新发布
07-15
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值