C++ 搜索下一个字符时出现意外行为

C++ 搜索下一个字符时出现意外行为,c++,string,C++,String,我有一个函数“int nextCharType(string s)”,它应该根据字符的“type”返回一个int 我将按几个自定义类型进行排序: 数组(搜索“[”) Json(搜索“{”) 引号(搜索“”) 编号(请参见isValidNumber) 我的职能如下: int nextCharType(const std::string& s) { char f; bool e; return nextCharType(s, f, e); } int nextC

我有一个函数“int nextCharType(string s)”,它应该根据字符的“type”返回一个int

我将按几个自定义类型进行排序:

  • 数组(搜索“[”)
  • Json(搜索“{”)
  • 引号(搜索“”)
  • 编号(请参见isValidNumber)
我的职能如下:

int nextCharType(const std::string& s)
{
    char f;
    bool e;
    return nextCharType(s, f, e);
}

int nextCharType(const std::string& s, char& foundChar, bool& error) 
{
    const char errorChar = 0x01;
    const char jsonOpeningBracket = '{';
    const char arrayOpeningBracket = '[';
    const char quoteChar = '"';

    foundChar = errorChar;
    error = false;

    if(s.length() == 0)
    {
        error = true;
        return 0;
    }

    bool foundNumeric = false;

    for(int i = 0; i < s.length() && i != s.npos; i++)
    {
        char c = s.at(i);
        if(c == '\\')
        {
            i++;
            continue;
        }

        if(c == arrayOpeningBracket || c == quoteChar || c == jsonOpeningBracket)
        {
            foundChar = c;
            break;
        }
        else if(((c == '-' || c == '.') && (i + 1 <= s.length() && isValidNumber(s.at(i + 1))))
            || /*number*/ isValidNumber(c)) // check for sign and decimal char
        {

            foundChar = c;
            foundNumeric = true;
            break;
        }
    }

    if(foundChar == errorChar)
    {
        error = true;
        return 0;
    }

    if(foundNumeric)
        return 4;

    switch(foundChar)
    {
        case jsonOpeningBracket:
            return 1;
            break;
        case arrayOpeningBracket:
            return 2;
            break;
        case quoteChar:
            return 3;
            break;
        default:
            error = true;
            return 0;
            break;
    }
}

bool isValidNumber(const string& num)
{
    if(num.empty())
        return false;
    if(countChar(num, '.') > 1) // countChar will simply count the given char in the string and return an int
        return false;
    if(countChar(num, '-') > 1)
        return false;

    std::size_t pos = 0;
    if(num.at(0) == '-')
    {
        pos = 1;
    }

    string internalNum = num;
    if(internalNum.find_first_not_of("0123456789.", pos) == internalNum.npos)
        return true;
    else
        return false;
}
让我困惑的是,我用一些单元测试来测试isValidNumber,如果我测试它,它就会像预期的那样工作

bool valid = isValidNumber(" "); // space - not valid
有人能帮忙吗?

  • “arrayOpeningBracket”未在此范围内声明
  • “quoteChar”未在此范围内声明
  • “jsonOpeningBracket”未在此范围内声明
有很多变量没有声明!所以首先声明


否则如果((c='-'| c='.)&(i+1解决方案是存在一个重载函数isValidNumber(double),在给定字符时调用它,而不是isValidNumber(string)。 删除后,添加新功能:

bool isValidNumber(char c) {
    return std::isdigit(c);
}

问题解决了。

&&i!=s.npos
-为什么?一个
int
永远不能接近
size\u t的最大值;
nextCharType(s);
会导致编译错误。您没有这样的函数。nextCharType(s)不会崩溃,因为我有一些别名函数,它们将提供所需的参数,只是不返回它们。我将更新我的question@Fureeish以前我在for循环中使用了std::site_t I-在调试时,我更改了它,因为没有真正的原因。您需要提供。您的代码现在显然没有编译。括号在下一行,变量在别处定义
bool isValidNumber(char c) {
    return std::isdigit(c);
}