簡體   English   中英

嘗試使用命令行參數的核心轉儲。 (C ++)

[英]Core Dump with trying to take command line arguments. (C++)

我一直在嘗試使用C ++命令行參數,但遇到了一些問題。 最初,我試圖使用“ ==”運算符將“ argv”與字符串進行比較。 我很快了解到,這是比較指針而不是值。 我修復了該錯誤,但現在在運行時得到了該錯誤。

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_S_construct null not valid
Aborted (core dumped)

該程序編譯良好,我也沒有從編譯器收到警告。 這是我的源代碼,以便您可以幫助我找到問題。

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    //Deal with arguments and send them to the correct functions
    if (argc >= 2){

        string op = argv[2];
        if (op == "-a" || op == "--automatic"){
            cout << "Test";
        }

        return 0;
    }
    //Or, just write help and info
    cout << "\n";
    cout << "bwc v0.0.1U-(Unstable)\n\n";
    cout << "Usage: bwc <operation> [...]\n";
    cout << "Operations:\n";
    cout << "   bwc {-a --automatic} <file(s)>\n";
    cout << "   bwc {-i --interactive}\n";
    cout << "   bwc {-c --error-codes}\n";
    cout << "\n";
    return 0;
}

您對argv[]索引減少了一個。 更改:

string op = argv[2];

至:

string op = argv[1];

暫無
暫無

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

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