gpt4 book ai didi

找不到函数的 C++ 错误标识符?

转载 作者:行者123 更新时间:2023-11-28 04:31:37 25 4
gpt4 key购买 nike

这是我在 C++ 中的第一步,我正在尝试运行我需要使用的函数

但是我得到这个错误:

Severity Code Description Project File Line Suppression State Error C3861 'findAndRotateBlankNumbers': identifier not found ConsoleApplication2 c:\users\source\repos\consoleapplication2\consoleapplication2\consoleapplication2.cpp 29

这是代码:

//ConsoleApplication2.cpp :定义控制台应用程序的入口点。//

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

enum States {
needBlank,
needNumber,
collectNumbers,
};

bool finish = false;
int i = 0;

int main()

{


char data[] = { "they're 2fast 96 345 6789 a11 24x 2424" };


printf(data);
findAndRotateBlankNumbers(data);
printf(data);

system("pause");





return 0;
}


void findAndRotateBlankNumbers(char* ptr) {
char* first;
char* last;
for (uint8_t state = needBlank; *ptr; ptr++) {
switch (state) {
case needBlank:
if (*ptr == ' ') {
state = needNumber;
}
break;
case needNumber:
if (isdigit(*ptr)) {
state = collectNumbers;
first = ptr;
}
else if (*ptr != ' ') {
state = needBlank;
}
break;
case collectNumbers:
{
bool swap = false;
if (!isdigit(*ptr)) {
last = ptr - 1;
state = (*ptr == ' ' ? needNumber : needBlank);
swap = true;
}
else if (!ptr[1]) {
last = ptr;
swap = true;
}
if (swap) {
if (last != first) {
for (int8_t nums = (last - first + 1) / 2; nums--; ) {
char swap = *first;
*first++ = *last;
*last-- = swap;
}
}
}
break;
}
}
}

}

这是什么?


我已经按照您的建议更改了代码的顺序:

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

enum States {
needBlank,
needNumber,
collectNumbers,
};

bool finish = false;
int i = 0;

void findAndRotateBlankNumbers(char* ptr) {
char* first;
char* last;
for (uint8_t state = needBlank; *ptr; ptr++) {
switch (state) {
case needBlank:
if (*ptr == ' ') {
state = needNumber;
}
break;
case needNumber:
if (isdigit(*ptr)) {
state = collectNumbers;
first = ptr;
}
else if (*ptr != ' ') {
state = needBlank;
}
break;
case collectNumbers:
{
bool swap = false;
if (!isdigit(*ptr)) {
last = ptr - 1;
state = (*ptr == ' ' ? needNumber : needBlank);
swap = true;
}
else if (!ptr[1]) {
last = ptr;
swap = true;
}
if (swap) {
if (last != first) {
for (int8_t nums = (last - first + 1) / 2; nums--; ) {
char swap = *first;
*first++ = *last;
*last-- = swap;
}
}
}
break;
}
}
}

}

int main()

{


char data[] = { "they're 2fast 96 345 6789 a11 24x 2424" };


printf(data);
findAndRotateBlankNumbers(data);
printf(data);

system("pause");





return 0;
}

但现在我得到了其他错误:

Severity Code Description Project File Line Suppression State Error C4703 potentially uninitialized local pointer variable 'first' used ConsoleApplication2 c:\users\source\repos\consoleapplication2\consoleapplication2\consoleapplication2.cpp 52

?

最佳答案

您的函数 findAndRotateBlankNumbers 是在您要在 main 中调用它的之后定义的。所以编译器在编译main函数的时候并不知道这个函数的存在。要解决此问题,您可以

  • findAndRotateBlankNumbers 函数移到上方 main
  • 在顶部添加函数声明并将定义保留在原处:void findAndRotateBlankNumbers(char* ptr);

关于你的第二个错误:

编译器提示您声明变量 first 而没有在初始化时为其赋值。你应该给它一个合理的默认值,比如 char* first = nullptr;

char* last = nullptr;

如果您的编译器不支持nullptr,请改用NULL

关于找不到函数的 C++ 错误标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52754975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com