尝试在类中使用 std::vector::push_back 时获取“_throw_bad_array_new_lengthv”

ghs*_*res 2 c++ runtime-error stdvector

我正在用 C++ 创建一个自定义语言解析器,我正在努力解决运行时错误,其中我有一个std::vector<std::string>带有构造函数的类的成员。完整的错误是:

The procedure entry point
_ZSt28__throw_bad_array_new_lengthv could no be located
in the dynamic link library
"path_to_executable"
Run Code Online (Sandbox Code Playgroud)

每当我尝试在代码中使用 std::vector::push_back 时,都会引发此错误,但 std::vector 不应该是动态大小的数据容器吗?为什么会出现这个错误?

我的一些代码:

The procedure entry point
_ZSt28__throw_bad_array_new_lengthv could no be located
in the dynamic link library
"path_to_executable"
Run Code Online (Sandbox Code Playgroud)
//"lib/cursor.h"

#ifndef T_CURSOR
#define T_CURSOR

#include <iostream>

struct Cursor
{
private:
    std::string input;
    int inputLength;

    char getChar(int p);

public:
    char character;
    int pos;
    int line;
    int column;
    bool eof;
    bool lineBreak;

    Cursor(std::string i);

    void walk(bool back = false);
    void walkTimes(int times, bool back = false);
    void move(int toPos);
    void skipIgnore();
    std::string toString();
};

#endif
Run Code Online (Sandbox Code Playgroud)
//"lib/cursor.cpp"

#include <sstream>
#include "cursor.h"

Cursor::Cursor(std::string i)
{
    this->input = i;
    this->inputLength = i.length();
    this->character = i.at(0);
    this->pos = 0;
    this->line = 0;
    this->column = 0;
    this->eof = false;
    this->lineBreak = false;
}

char Cursor::getChar(int pos)
{
    if (pos < 0 || pos >= this->inputLength)
    {
        return EOF;
    }
    return this->input.at(pos);
}

void Cursor::walk(bool back)
{
    if (back)
    {
        this->pos--;
        this->column--;
        if (this->lineBreak)
        {
            this->line--;
            this->column = 0;
            for (int i = this->pos - 1; i >= 0; i--)
            {
                if (this->getChar(i) == '\n')
                    break;
                this->column++;
            }
        }
    }
    else
    {
        this->pos++;
        this->column++;
        if (this->lineBreak)
        {
            this->line++;
            this->column = 0;
        }
    }
    this->character = this->getChar(this->pos);
    this->eof = this->character == EOF;
    this->lineBreak = this->character == '\n';
}

void Cursor::walkTimes(int times, bool back)
{
    for (int i = 0; i < times; i++)
    {
        this->walk(back);
    }
}

void Cursor::move(int pos)
{
    if (pos < 0)
        pos = 0;
    if (pos > this->inputLength - 1)
        pos = this->inputLength - 1;

    this->pos = 0;
    this->character = this->input.at(0);
    this->line = 0;
    this->column = 0;
    this->eof = false;
    this->lineBreak = this->character == '\n';

    while (this->pos < pos)
        this->walk();
}

void Cursor::skipIgnore()
{
    while (this->character == ' ' ||
                 this->character == '\n' ||
                 this->character == '\t')
        this->walk();

    if (this->character == '#')
    {
        while (!this->eof && this->character != '\n')
            this->walk();
    }

    while (this->character == ' ' ||
                 this->character == '\n' ||
                 this->character == '\t')
        this->walk();
}

std::string Cursor::toString()
{
    std::stringstream ss("");
    ss << "(P:" << this->pos;
    ss << " L:" << this->line;
    ss << " C:" << this->column;
    ss << " \"" << this->character << "\")";

    return ss.str();
}
Run Code Online (Sandbox Code Playgroud)
//"lib/lexer.h"
#ifndef T_LEXER
#define T_LEXER

#include <iostream>
#include <vector>
#include "cursor.h"

class Lexer
{
private:
    std::string input;

protected:
    Cursor cursor;
    std::vector<std::string> matchStack;
    std::vector<std::vector<Cursor>> cursorStack;

public:
    Lexer(std::string input);

    std::string getStr(int pos);
    void setStr(int pos, std::string str);
    Cursor getCursorStart(int pos);
    Cursor getCursorEnd(int pos);

    bool match(std::string str);
};

#endif
Run Code Online (Sandbox Code Playgroud)
//"lib/lexer.cpp"
#include "lexer.h"

Lexer::Lexer(std::string input) : cursor(input)
{
    this->input = input;
}

std::string Lexer::getStr(int pos)
{
    if (this->matchStack.size() == 0)
        return this->input;
    while (pos < 0)
        pos += this->matchStack.size();
    while (pos >= this->matchStack.size())
        pos -= this->matchStack.size();

    return this->matchStack[pos];
}

void Lexer::setStr(int pos, std::string str)
{
    if (this->matchStack.size() == 0)
        return;
    while (pos < 0)
        pos += this->matchStack.size();
    while (pos >= this->matchStack.size())
        pos -= this->matchStack.size();

    this->matchStack[pos] = str;
}

Cursor Lexer::getCursorStart(int pos)
{
    if (this->cursorStack.size() == 0)
        return Cursor(this->input);
    while (pos < 0)
        pos += this->cursorStack.size();
    while (pos >= this->cursorStack.size())
        pos -= this->cursorStack.size();

    return this->cursorStack[pos][0];
}

Cursor Lexer::getCursorEnd(int pos)
{
    if (this->cursorStack.size() == 0)
        return Cursor(this->input);
    while (pos < 0)
        pos += this->cursorStack.size();
    while (pos >= this->cursorStack.size())
        pos -= this->cursorStack.size();

    return this->cursorStack[pos][1];
}

bool Lexer::match(std::string str)
{
    this->cursor.skipIgnore();

    const std::string ss = this->input.substr(this->cursor.pos, str.length());

    if (ss == str)
    {
        this->matchStack.push_back(ss); // Getting error if I include this line

        const Cursor startCursor = this->cursor;
        this->cursor.walkTimes(str.length());
        const Cursor endCursor = this->cursor;
        this->cursorStack.push_back({startCursor, endCursor});

        return true;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

我正在 Windows 上使用 g++ 编译我的程序:g++ test.cpp lib/cursor.cpp lib/lexer.cpp -o test.exe

Thi*_*101 7

使用 g++ 在命令行中编译时出现同样的错误。但它在代码块中运行良好。要解决此问题,请尝试使用 进行编译-static-libstdc++。它应该可以解决问题。