【问题标题】:c++ error identifier not found for function?找不到函数的c ++错误标识符?
【发布时间】: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


【解决方案1】:

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

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

关于你的第二个错误:

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

char* last = nullptr;相同

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

【讨论】:

  • 我做到了,出现了新错误 - 我编辑了帖子 - 见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 2013-12-31
  • 2013-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多