簡體   English   中英

提升正則表達式運行時錯誤

[英]Boost regex runtime error

我正在嘗試使用我在另一台將字符串拆分為標記的計算機上編寫的代碼。 這段代碼編譯得很好。 該代碼也可以在其他一些計算機上運行。 我已設法將代碼減少到以下內容:

#include <string>
#include <boost/regex.hpp>

typedef std::vector<std::string> token_t ;

token_t generate_tokens(std::string raw_input){ 
//this function breaks a input string into tokens. So test 100 goes to 2 tokens "test" and "100".

    boost::regex re_splitter("\\s+"); //this uses the regex \s+ to find whitespace. The + finds one or more whitespace characters.

    boost::sregex_token_iterator iter(raw_input.begin(), raw_input.end(), re_splitter, -1);
    //this breaks the string using the regex re_splitter to split into tokens when that is found. 
    boost::sregex_token_iterator j; //This is actually in the Boost examples, j is the equivalent of end. Yes this did also seem weird to me at first...

    token_t token_vector;
    unsigned int count = 0;
    while(iter != j)
    {
        token_vector.push_back(*iter);
        std::cout << *iter++ << std::endl;
        ++count;
    }
    return token_vector;
}

int main(){
    std::string in;
    int amount = -1;

    std::cout << "action: ";
    std::getline(std::cin, in);

    boost::regex EXPR("^test \\d*(\\.\\d{1,2})?$");
    bool format_matches = boost::regex_match(in, EXPR);

    token_t tokens = generate_tokens(in);

    if(format_matches){
        amount = atoi(tokens.at(1).c_str());
    }
    std::cout << "amount: " << amount << "\n";
    return 0;
}

這使用以下命令編譯時沒有錯誤或警告: g++ -Wall test.cpp -lboost_regex但是在運行時使用時提供輸入test 100程序失敗。

行動:測試100

a.out:/usr/local/include/boost/smart_ptr/shared_ptr.hpp:412:typename boost :: detail :: shared_ptr_traits :: reference boost :: shared_ptr :: operator *()const [with T = boost :: regex_traits_wrapper >>]:斷言`px!= 0'失敗。

中止

我完全迷失了這里發生的事情。 這是我的代碼或庫中的錯誤嗎? 任何有關調試的建議都非常感謝!

那不是錯誤 它是boost頭文件的沖突。

這可能是因為文件包含錯誤,或者包含錯誤的庫(正則表達式模塊是少數需要編譯的boost模塊之一)。

您應該使用-l和-I開關確保使用正確的文件,例如:

   g++ -W -Wall main.cpp $(LDFLAGS) -lboost_regex -I/data1/PROJECT_SEARCH/libsrc/boost_1_46_1

當您使用boost版本的頭文件進行編譯並使用另一個boost版本執行時,會發生這種情況。 您應該檢查安裝哪個boost庫以便執行,以及用於編譯的庫。

在gdb或類似程序中運行此命令,在這些部分的開頭設置斷點,然后逐步執行直到找到有問題的行。

你得到的錯誤看起來像一個無效的指針正被傳遞到某個地方的boost庫。

因為你沒有在你的代碼中使用shared_ptr ,而且我看不到其他看起來錯誤的東西並且它在其他機器上工作,我會說它可能是Boost.Regex中的一個錯誤。

我打賭其他機器安裝了其他版本的boost?

如果我不得不猜測我會嘗試更改std::cout << *iter++ << std::endl; 先行。 - > std::cout << *iter << std::endl; ++iter; std::cout << *iter << std::endl; ++iter;

是的,在瑞士建議的調試器中運行它,看看斷言被觸發的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM