string : undeclared identifier 出错原因

本文介绍了一个常见的编程错误——使用string类型时出现的'string':undeclaredidentifier错误,并提供了正确的解决方案:在引入string头文件的同时指定std命名空间。

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

 

在使用string类型时,添加了<string>头文件,编译后出现

'string' : undeclared identifier错误,究其原因主要是没有给出命名空间。

正确的是:

 #include <string> 
using   namespace   std;

../Core/Src/main.c:75:12: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token 75 | Uart1_State.huart = &huart1; | ^ ../Core/Src/main.c:76:12: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token 76 | Uart2_State.huart = &huart2; | ^ ../Core/Src/main.c: In function 'main': ../Core/Src/main.c:150:5: warning: implicit declaration of function 'uart_send_string'; did you mean 'usart2_send_lora_string'? [-Wimplicit-function-declaration] 150 | uart_send_string(&Uart1_State, uTxBuffer, TXBUFFERSIZE); | ^~~~~~~~~~~~~~~~ | usart2_send_lora_string ../Core/Src/main.c: At top level: ../Core/Src/main.c:350:6: warning: conflicting types for 'uart_send_string'; have 'void(UART_State *, uint8_t *, uint16_t)' {aka 'void(UART_State *, unsigned char *, short unsigned int)'} 350 | void uart_send_string(UART_State *state, uint8_t *buffer, uint16_t size) { | ^~~~~~~~~~~~~~~~ ../Core/Src/main.c:150:5: note: previous implicit declaration of 'uart_send_string' with type 'void(UART_State *, uint8_t *, uint16_t)' {aka 'void(UART_State *, unsigned char *, short unsigned int)'} 150 | uart_send_string(&Uart1_State, uTxBuffer, TXBUFFERSIZE); | ^~~~~~~~~~~~~~~~ ../Core/Src/main.c: In function 'uart_send_string': ../Core/Src/main.c:364:29: error: 'USART_TypeDef' has no member named 'DR'; did you mean 'RDR'? 364 | state->huart->Instance->DR = buffer[0]; | ^~ | RDR make: *** [Core/Src/subdir.mk:49: Core/Src/main.o] Error 1 make: *** Waiting for unfinished jobs.... ../Core/Src/stm32f0xx_it.c: In function 'USART1_IRQHandler': ../Core/Src/stm32f0xx_it.c:234:13: error: 'Uart1_State' undeclared (first use in this function) 234 | if (Uart1_State.Tx_Count < Uart1_State.Tx_Size) { | ^~~~~~~~~~~ ../Core/Src/stm32f0xx_it.c:234:13: note: each undeclared identifier is reported only once for each function it appears in ../Core/Src/stm32f0xx_it.c:235:30: error: 'USART_TypeDef' has no member named 'DR'; did you mean 'RDR'? 235 | huart1.Instance->DR = Uart1_State.Tx_Buffer[Uart1_State.Tx_Count++]; | ^~ | RDR ../Core/Src/stm32f0xx_it.c: In function 'USART2_IRQHandler': ../Core/Src/stm32f0xx_it.c:262:11: error: 'Uart2_State' undeclared (first use in this function) 262 | if (Uart2_State.Tx_Count < Uart2_State.Tx_Size) { | ^~~~~~~~~~~ ../Core/Src/stm32f0xx_it.c:263:28: error: 'USART_TypeDef' has no member named 'DR'; did you mean 'RDR'? 263 | huart2.Instance->DR = Uart2_State.Tx_Buffer[Uart2_State.Tx_Count++]; | ^~ | RDR make: *** [Core/Src/subdir.mk:49: Core/Src/stm32f0xx_it.o] Error 1 "make -j12 all" terminated with exit code 2. Build might be incomplete.
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值