【发布时间】:2018-10-11 07:52:44
【问题描述】:
这是我在 C++ 中的第一步,我正在尝试运行我需要使用的函数
但我收到此错误:
严重性代码描述项目文件行抑制状态 错误 C3861 'findAndRotateBlankNumbers':标识符不是 找到 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;
}
但现在我得到了其他错误:
严重性代码描述项目文件行抑制状态 错误 C4703 可能未初始化的局部指针变量 'first' 使用 ConsoleApplication2 c:\users\source\repos\consoleapplication2\consoleapplication2\consoleapplication2.cpp 52
?
【问题讨论】:
-
使用前需要声明。
-
你需要在调用之前声明你的函数。将其移至
main上方,或通过将void findAndRotateBlankNumbers(char* ptr);放在main之前声明它。 -
我把函数放在 main 函数之前,但现在我得到了其他错误: 严重性代码描述项目文件行抑制状态错误 C4703 可能未初始化的本地指针变量 'first' used ConsoleApplication2 c:\users\source\ repos\consoleapplication2\consoleapplication2\consoleapplication2.cpp 52
-
@David12123 编译器清楚地用简单的英语告诉
potentially uninitialized local pointer variable 'first'。
标签: c++ function compiler-errors