奇怪的 gdb c++ 错误

Strange gdb c++ error

我正在用 C++ 编写命令行程序,运行 遇到了一些奇怪的问题。 我是 C++ 的新手,所以这可能就是我如此困惑的原因。 我正在使用MinGW编译调试

这是当前代码:

#include <string>
#include <vector>
#include <iostream>
#include <stdlib.h> 

using namespace std;

// Global Vars
string headerSpace = "\t\t";
string instructionSpace = "\t";
string prmpt = "WIO:+>";
string daysOFWeek [7] = {"mon", "tue", "wed", "thur", "fri", "sat", "sun"};

vector<string> commandHelpGet ("To view a day's exercises type:: get (date)\n", "To view today's required exercises type:: get required\n");
vector<string> commandHelpRem ( "To clear a day's record type:: remove (date)\n", "To remove an exercise from your routine type:: remove (name)\n");
vector<string> commandHelpRec ("To record a day's exercises type:: record (date)\n");
vector<string> commandHelpAdd ("To add an exercise to your routine type:: add (name) (rep/min) (daysOFWeek)\n");




// Method prototypes 
vector<string> splitString(string input, string splitter);
void processGetCom(vector<string> input);
void processRecordCom(vector<string> input);
void processRemoveCom(vector<string> input);
void processAddCom(vector<string> input);
void help(string context);
void printError(string reason);
string requiredExercises();
string getRecord(string input);


int main()
{
    cout << "\n\n" + headerSpace + "---------- Welcome to WorkItOut! ----------\n";
    help("");
    string inRaw = "";
    vector<string> input;
    while (true)
    {
        cout << prmpt; 
        string inRaw = "";
        getline(cin, inRaw);
        input = splitString(inRaw, " ");

        // Overflow guard
        if (input.size() > 4)
        {
            printError("To many arguments; Please enter a valid command");
        }

        if (input[0].compare("get") == 0)
        {
            processGetCom(input);
        }
        else if (input[0].compare("record") == 0)
        {
            processRecordCom(input);
        }
        else if (input[0].compare("remove") == 0)
        {
            processRemoveCom(input);
        }
        else if (input[0].compare("add") == 0)
        {
            processAddCom(input);
        }
        else if (input[0].compare("exit") == 0)
        {
            exit(0);
        }
    }
}


/* Updated C. A. Acred on 05/22/2015
 * This method takes a string and splits it into a vector with the second input.
 */
vector<string> splitString(string input, string splitter)
{
    vector<string> output; // initialize output vector
    int currentElement = 0; // set current vector element to 0
    string currentChar; // declare a container for the current char
    output.push_back(""); // Push an empty string onto output
    for(int i = 0; i < input.length(); i++) // for each char in input
    {
        currentChar = input.substr(i, 1); // currentChar = the char in input @ i
        if (currentChar.compare(splitter) == 0) // if this char and the splitter are the same
        {
            while (currentChar.compare(splitter) == 0) // while they are the same
            {
                i++; // advance i by 1
                currentChar = input.substr(i, 1); // get the next char
            }
            output.push_back(currentChar); // put the current char as the first in a new element
            currentElement++; // point to the new element
        }
        else
        {
            output[currentElement] = output[currentElement] + currentChar; // output @ currentElement += currentChar
        }
    }
    return output;
}

string requiredExercises()
{
    return "yo";
}


/* Updated C. A. Acred on 5/27/15
 * Processes get commands.
 */
void processGetCom(vector<string> input)
{
    if (input.size() > 2)
    {
        printError("To many arguments; Enter a valid command");
    }

    if (input[1].compare("required") == 0)
    {
        cout << requiredExercises();
    }
    else
    {
        cout << getRecord(input[1]);
    }
}

void processRecordCom(vector<string> input)
{

}

void processRemoveCom(vector<string> input)
{

}

void processAddCom(vector<string> input)
{

}

void printError(string reason)
{

}


/** Updated C. A. Acred 05/22/2015
 * Print help on commands.
 */
void help(string context)
{
    if (context.empty())
    {
        int i;
        for (i = 0; i < commandHelpGet.size(); i++)
        {
            cout << commandHelpGet[i];
        }
        for (i = 0; i < commandHelpRec.size(); i++)
        {
            cout << commandHelpRec[i];
        }
        for (i = 0; i < commandHelpRem.size(); i++)
        {
            cout << commandHelpRem[i];
        }
        for (i = 0; i < commandHelpAdd.size(); i++)
        {
            cout << commandHelpAdd[i];
        }
    }
    cout << instructionSpace + "To exit type:: exit\n";
    cout << instructionSpace + "-+| date format = m/d/yr |+-\n\t\tExample:: 05/20/2015\n";
    cout << instructionSpace + "-+| daysOFWeek format = day/day/day..etc(up to 7) |+-\n\t\tExample:: mon/tue/wed/thur/fri/sat/sun\n\n\n\n";
    cout << instructionSpace + "-+| rep format = rep:(#) |+-\n\t\tExample:: rep:15\n\n\n\n";
    cout << instructionSpace + "-+| min format = min:(#) |+-\n\t\tExample:: min:15\n\n\n\n";
}

这是我从 g++ 得到的奇怪错误:

main.cpp:24:84: error: invalid conversion from 'const char*' to 'unsigned int' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:263:7: error:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_stri
ng<char> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]' [-fpermissive]
main.cpp:25:111: error: invalid conversion from 'const char*' to 'unsigned int' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:263:7: error:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_stri
ng<char> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]' [-fpermissive]
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/vector:63:0,
                 from main.cpp:10:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_construct.h: In function 'void std::_Construct(_T1*, const _T2&) [with _T1 = std::basic_string<char>, _T2 = char]':
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:77:3:   instantiated from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*, _ForwardIterator = std::bas
ic_string<char>*, bool _TrivialValueTypes = false]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:119:41:   instantiated from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*, _ForwardIterator = std::basic_string<char>*]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:259:63:   instantiated from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const char*, _ForwardIterator = std::basic_string<char>*
, _Tp = std::basic_string<char>]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1113:4:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::alloca
tor<std::basic_string<char> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1091:4:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::allocator<std::ba
sic_string<char> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:340:4:   instantiated from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >
, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]'
main.cpp:22:140:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_construct.h:84:7: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.tcc:214:5: error:   initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermiss
ive]

非常感谢您帮助理解和修复此错误。

更新#1:

我更改了向量 commandHelpGet - commandHelpAdd 的初始化以使用花括号,但现在我收到一个新错误:

main.cpp:22:16: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:22:139: error: in C++98 'commandHelpGet' must be initialized by constructor, not by '{...}'
main.cpp:23:16: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:23:147: error: in C++98 'commandHelpRem' must be initialized by constructor, not by '{...}'
main.cpp:24:16: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:24:84: error: in C++98 'commandHelpRec' must be initialized by constructor, not by '{...}'
main.cpp:24:84: error: invalid conversion from 'const char*' to 'unsigned int' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:263:7: error:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_stri
ng<char> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]' [-fpermissive]
main.cpp:25:16: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:25:111: error: in C++98 'commandHelpAdd' must be initialized by constructor, not by '{...}'
main.cpp:25:111: error: invalid conversion from 'const char*' to 'unsigned int' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:263:7: error:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::vector(std::vector<_Tp, _Alloc>::size_type, const value_type&, const allocator_type&) [with _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_stri
ng<char> >, std::vector<_Tp, _Alloc>::size_type = unsigned int, std::vector<_Tp, _Alloc>::value_type = std::basic_string<char>, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]' [-fpermissive]
In file included from c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/vector:63:0,
                 from main.cpp:10:
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_construct.h: In function 'void std::_Construct(_T1*, const _T2&) [with _T1 = std::basic_string<char>, _T2 = char]':
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:77:3:   instantiated from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*, _ForwardIterator = std::bas
ic_string<char>*, bool _TrivialValueTypes = false]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:119:41:   instantiated from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const char*, _ForwardIterator = std::basic_string<char>*]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_uninitialized.h:259:63:   instantiated from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const char*, _ForwardIterator = std::basic_string<char>*
, _Tp = std::basic_string<char>]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1113:4:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::alloca
tor<std::basic_string<char> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:1091:4:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_initialize_dispatch(_InputIterator, _InputIterator, std::__false_type) [with _InputIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::allocator<std::ba
sic_string<char> >]'
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_vector.h:340:4:   instantiated from 'std::vector<_Tp, _Alloc>::vector(_InputIterator, _InputIterator, const allocator_type&) [with _InputIterator = const char*, _Tp = std::basic_string<char>, _Alloc = std::allocator<std::basic_string<char> >
, std::vector<_Tp, _Alloc>::allocator_type = std::allocator<std::basic_string<char> >]'
main.cpp:22:139:   instantiated from here
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/stl_construct.h:84:7: error: invalid conversion from 'char' to 'const char*' [-fpermissive]
c:\mingw\bin\../lib/gcc/mingw32/4.6.2/include/c++/bits/basic_string.tcc:214:5: error:   initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermiss
ive]

- CAA14

看看你的矢量初始化。如果您打算使用列表语法,请使用大括号代替括号:

vector<string> commandHelpGet { "To view a day's exercises type:: get (date)\n", "To view today's required exercises type:: get required\n" };

看例子here