gpt4 book ai didi

c++ - 字符串 Vector push_back 在类里面失败

转载 作者:搜寻专家 更新时间:2023-10-30 23:58:42 26 4
gpt4 key购买 nike

我有一个类,其方法应返回一个字符串 vector 。 getCommVector 方法必须将字符串数组的元素 push_back 到一个字符串 vector 中,然后该方法可以返回该 vector 。尝试将字符串元素添加到字符串 vector 时,我得到:

libc++abi.dylib: terminate called throwing an exception
2Program ended with exit code: 0

我不明白为什么我不能将字符串 push_back 到 vector 。有任何想法吗?提前致谢!

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

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)
{

}
std::vector<std::string> Command::getCommVector ()
{
std::string s ="test";
finalCommVector.push_back("s");
return 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;
}

最佳答案

您正在迭代超出数组大小的方式。 sizeof(commVector) 返回数组的大小以字节为单位。

如果你有可用的 C++11,你可以这样做:

for (const auto &s : commVector) {
if (s != "") {
// as before
}
}

或者至少这个(如果你只有部分 C++11 支持):

for (auto it = std::begin(commVector); it != std::end(commVector); ++it) {
std::string s = *it;
// the rest as before
}

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

for (int i = 0; i < sizeof(commVector) / sizeof(commVector[0]); ++i) {
// the rest as before
}

或者提供您自己的函数来获取正确的数组大小:

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
}

关于c++ - 字符串 Vector push_back 在类里面失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19360463/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com