C++ 字符串向量推回在类中失败

C++ 字符串向量推回在类中失败,c++,class,vector,C++,Class,Vector,我有一个类,它的方法应该返回字符串向量。getCommVector方法必须将字符串数组的元素推回字符串向量,然后该方法返回该字符串向量。尝试将字符串元素添加到字符串向量时,我得到: libc++abi.dylib: terminate called throwing an exception 2Program ended with exit code: 0 我不明白为什么我不能把字符串推回向量。有什么想法吗? 提前谢谢 感兴趣的代码段(根据建议编辑): class命令{ 公众: //命令(st

我有一个类,它的方法应该返回字符串向量。getCommVector方法必须将字符串数组的元素推回字符串向量,然后该方法返回该字符串向量。尝试将字符串元素添加到字符串向量时,我得到:

libc++abi.dylib: terminate called throwing an exception
2Program ended with exit code: 0
我不明白为什么我不能把字符串推回向量。有什么想法吗? 提前谢谢

感兴趣的代码段(根据建议编辑):

class命令{
公众:
//命令(std::string,bool,bool);
void setOperators(std::string、bool、bool);
无效分析命令();
命令();
std::vector getCommVector();
私人:
int numOperators;//总命令数
int optcount;//当前命令号
std::字符串输入\ u字符串;
bool field_命令、byte_命令;
std::string commVector[3];
std::向量finalCommVector;
无效字节_分析();
空洞场分析();
void decode_命令();
无效语法错误();
无效解码_错误();
};
Command::Command():numOperators(0)、opCount(0)、field_命令(false)、byte_命令(false)
{
}
std::vector命令::getCommVector()
{
std::string s=“测试”;
FinalComVector.向后推(“s”);
返回finalCommVector;
}
添加SSCE:

class Command {
    public:
        //Command (std::string, bool, bool);
        void setOperators(std::string,bool, bool);
        void analyseCommand();
        Command();
        std::vector<std::string> getCommVector ();
    private:
        int numOperators; //number of total commands
        int opCount; //current command number
        std::string input_string;
        bool field_command, byte_command;
        std::string commVector[3];
        std::vector<std::string> finalCommVector;
        void byte_analysis();
        void field_analysis();
        void decode_command();
        void syntax_error();
        void decode_error();

};


Command::Command() : numOperators(0), opCount(0), field_command(false),byte_command(false)
{

}

void Command::syntax_error()
{
    std::cout<<"Incorrect Syntax Error: Usage: linuxcut -b num -f num \n";
    exit(EXIT_FAILURE);
}

void Command::decode_error()
{
    std::cout<<"Decode Error: Usage: linuxcut -b num -f num \n";
    exit(EXIT_FAILURE);
}


void Command::analyseCommand()
{
    if (byte_command) {
        //start byte command analysys
        byte_analysis();
    }
    else if (field_command)
    {
        //start field command analysys
        field_analysis();
    }
}




void Command::setOperators(std::string input_argument, bool is_field, bool is_byte)
{
    input_string = input_argument;

    field_command = is_field;
    byte_command = is_byte;
}

std::vector<std::string> Command::getCommVector ()
{

    std::string s = "ssd";
    finalCommVector.push_back(s);
    /*
    for (int i = 0; i<sizeof(commVector); i++)
    {

        if (commVector[i] != "")
        {
            //debug

            std::cout<<"asdas";
        }


    }
     */
    return finalCommVector;
}


void Command::byte_analysis()
{
    int next_state = 0;
    int dashCount = 0;
    int commVectIndex = 0;




    //iterate through string and check if the argument is valid
    for (int i= 0; i<input_string.length(); i++) {



        switch (next_state) {
            case 0: //start
                //if character is a number:
                if (isdigit(input_string.at(i)))
                {
                    //first elemnt of command commVector is number
                    commVector[commVectIndex]+=input_string.at(i);

                    //DEBUG
                    std::cout<<commVector[commVectIndex];
                    next_state = 1;
                }
                //if character is a dash:
                else if (input_string[i] == '-')
                {
                    //increment dashCount
                    dashCount++;
                    //if next character in input_string is a number continue
                    if (isdigit(input_string[i+1])) {
                        commVector[commVectIndex]+=input_string.at(i);
                        commVectIndex++;
                        next_state = 1;
                    }
                    else //else error
                    {
                        syntax_error();
                    }
                }
                //if it's niether: error!
                else
                {

                    syntax_error();
                }
                break;

            case 1:
                //if next character is a number:
                if (isdigit(input_string[i]))
                {
                    commVector[commVectIndex]+=input_string.at(i);
                    next_state = 1;
                }
                //if next character is dash
                else if (input_string[i] == '-'&& dashCount <= 3)
                {
                    dashCount++;
                    //increment commandVectIndex
                    commVectIndex++;
                    next_state = 2;
                    commVector[commVectIndex]+=input_string.at(i);
                    //increment commandVectIndex to accomodate next operation
                    commVectIndex++;
                }

                //if it's niether: error!
                else
                {

                    syntax_error();
                }
                break;

            case 2://previous character was dash
                //if next character is number
                if (isdigit(input_string[i]))
                {
                    commVector[commVectIndex]+=input_string.at(i);
                    next_state = 1;
                }

                //if it's niether: error!
                else
                {

                    syntax_error();
                }
                break;

            default:
                syntax_error();
                break;
        }

    }

}



void Command::field_analysis()
{

}
/*****************FUNCTIONS DEFINITIONS***************/

void print_usage() {
    std::cout<<"Incorrect Syntax Error: Usage: linuxcut -b num -f num \n";
}



/*****************END OF FUNCTIONS DEFINITIONS***************/


/***************** MAIN ***************/

int main(int argc, char *argv[]) {
    int opt= 0;
    std::string byte = "-1-2,2",field = "";
    std::string sub_arg_delimiter = ","; //delimiter for comma serparated arguments
    static bool complement = false;
    int diffOpt = 0; //stores the difference between optind  and argc to read filenames in command
    std::string fileName;
    //Specifying the expected options
    //The two options l and b expect numbers as argument
    static struct option long_options[] = {
        {"byte",      required_argument,       0,  'b' },
        {"field", required_argument,       0,  'f' },
        {"complement",    no_argument, 0,  0 },
        {0,           0,                 0,  0   }
    };

    Command testCommand;
    testCommand.setOperators("-2-", false, true);

    std::vector<std::string> trial = testCommand.getCommVector();


    std::cout<<"filename:"<<fileName<<std::endl;
    std::cout<<"Selected flags:\n"<< "b: "<< byte<<"\nf: "<<field<<"\ncomplement: "<<complement<<std::endl;


    return 0;
}
class命令{
公众:
//命令(std::string,bool,bool);
void setOperators(std::string、bool、bool);
无效分析命令();
命令();
std::vector getCommVector();
私人:
int numOperators;//总命令数
int optcount;//当前命令号
std::字符串输入\ u字符串;
bool field_命令、byte_命令;
std::string commVector[3];
std::向量finalCommVector;
无效字节_分析();
空洞场分析();
void decode_命令();
无效语法错误();
无效解码_错误();
};
Command::Command():numOperators(0)、opCount(0)、field_命令(false)、byte_命令(false)
{
}
void命令::语法错误()
{

std::cout您的迭代超出了数组大小。
sizeof(commVector)
返回数组大小(以字节为单位)

如果您有可用的C++11,则可以执行以下操作:

for (const auto &s : commVector) {
  if (s != "") {
    // as before
  }
}
或者至少是这样(如果您只有部分C++11支持):

如果没有C++11,您至少可以这样做:

for (int i = 0; i < sizeof(commVector) / sizeof(commVector[0]); ++i) {
  // the rest as before
}
for(int i=0;i
或者提供您自己的函数以获得正确的数组大小:

template <class T, size_t N>
size_t arraySize(const T (&)[N]) { return N; }

// Use:

for (size_t i = 0; i < arraySize(commVector); ++i) {
  // the rest as before
}
模板
size_t arraySize(const t(&)[N]){return N;}
//使用:
对于(大小i=0;i

我不讨论在C++中使用宏:< /P> 当然,你也可以使用常量,因为你的数组有固定数量的元素,但我想这只是一个例子

sizeof
返回对象(在本例中为字符串数组)本身的大小,而不是向量中的元素计数

正因为如此,它等于数组元素数乘以单个字符串实例的大小,所以您可以尝试使用
操作符[]
访问不存在的项

这也被打破了:

finalCommVector.push_back("s"); 
也许你的意思是:

finalCommVector.push_back(s);

如果您只需要将
std::string commVector
数组作为
std::vector
,则可以使用
std::vector::assign

finalCommVector.assign(commVector,commVector+3)


“3”是数组的长度。

sizeof(commVector)
生成
3*sizeof(std::string)
,以字节为单位的大小,而不是元素数。这是否可能是
finalCommVector。推回(s)
没有撇号?谢谢,我现在要解决这个问题。我试着去掉那个语句,只是试着做一个简单的推回字符串的finalCom向量,但它失败了(没有循环或任何东西),而且仍然失败:(非常感谢,不幸的是,这并不能解决我的push_-back问题。我现在只是出于测试目的而取消了循环,但仍然失败。我将方法简化为:std::string s=“ssd”;finalCommVector.push_-back);@realn00b那么问题可能在其他地方。你能发布一个吗?整个代码很长,但在一个源文件中,我可以发布到哪里?@realn00b不可以。如果你想让我们花时间帮助你,请准备好先投资一些自己删除不相关的位。也许这甚至可以帮助你自己找到问题。这就是w为什么你应该在开始询问之前准备一份SSCCE。@realn00b哎哟,这是相当多的代码。你不能进一步减少它吗(例如,硬编码所需的选项,而不是解析它们等等)哦,是的,我混淆了FinalComVector。谢谢,我将修复答案
分配
替换内容,因此行为不同于
推回
(但不确定OP想要哪一个)我已经尝试过分配,但当我进入分配函数时,它陷入了无限循环中?您的程序中的其他地方有问题。请提供一个示例,您可以在其中演示您的问题。我可以在哪里上载它?很抱歉,第一次我必须上载SSCE在为
选项添加合适的定义后,您的程序运行无缺陷SLY
i<sizeof(commVector);
i<countof(commVector);
#define countof(a) (sizeof(a)/sizeof(a[0]))
finalCommVector.push_back("s"); 
finalCommVector.push_back(s);