C++ 试图从类数组输出字符串时发生std::length_错误

C++ 试图从类数组输出字符串时发生std::length_错误,c++,arrays,string,class,c++11,C++,Arrays,String,Class,C++11,我的程序由三个文件组成。arithcard.hpp(头)、assign2.cpp(主程序)和arithcard.cpp(方法) 在数组cards中,存储从文本文件读取的字符串。例如,cards[0]。fileLine的字符串为2+2=4。(fileLine是包含所有字符串的私有成员变量),cards[1]。fileLine包含字符串2+4=3,以此类推 我知道cards[I].fileLine包含所有这些字符串,因为我通过在readCards方法中将它们全部打印出来进行测试,以查看它们是否确实在

我的程序由三个文件组成。arithcard.hpp(头)、assign2.cpp(主程序)和arithcard.cpp(方法)

在数组cards中,存储从文本文件读取的字符串。例如,cards[0]。fileLine的字符串为2+2=4。(fileLine是包含所有字符串的私有成员变量),cards[1]。fileLine包含字符串2+4=3,以此类推

我知道cards[I].fileLine包含所有这些字符串,因为我通过在readCards方法中将它们全部打印出来进行测试,以查看它们是否确实在其中

当我尝试在主程序中调用cards[I].getQuestion(或.getAnswer())时,我的问题就在这里。有时,它会告诉我问题(或答案)或错误

“在抛出'std::length_error'实例后调用terminate” what():基本\u字符串::\u S\u创建”

arithcard.hpp
#包括
#包括
/*
定义类ArithCard,用于处理算术闪存卡。
您不能修改下面提供的公共方法声明。主要
用于测试解决方案的程序将假定这些方法存在。
您可以根据需要添加私有属性和方法。
您可能会发现添加额外的公共测试方法很方便。
请记住,您的代码必须与未修改的主程序一起工作
否则它将无法通过自动测试。
*/
类算术卡片{
公众:
//默认构造函数
ArithCard();
静态布尔读卡(const std::string和fileName,
int&cardCnt、ArithCard*&cards);
无效显示问题(标准::ostream&out);
bool checkAnswer(标准::istream&in);
//以字符串形式返回问题。
std::string getQuestion();
//以字符串形式返回答案。
std::string getAnswer();
//以整数形式返回答案。
int getAnswerValue();
私人:
//在此处添加您的私有方法和属性。
std::字符串文件行;
字符串问题;
字符串应答;
} ;
arithcard.cpp
#包括
#包括
#包括
#包括“arithcard.hpp”
使用名称空间std;
ArithCard::ArithCard()
{
//身体故意空着
}
bool ArithCard::readCards(常量字符串和文件名、int和cardCnt、ArithCard*&卡片)
{
//逐行阅读文件并将其存储在卡片中
返回(真);
}
无效显示问题(标准::ostream&out)
{
//显示输出
}
bool checkAnswer(标准::istream&in)
{
返回(真);
}
//以字符串形式返回问题。
字符串ArithCard::getQuestion()
{
大小\u t pos=0;
pos=fileLine.find(“=”);
问题=文件行.substr(0,位置);
返回问题;
}
//以字符串形式返回答案。
字符串ArithCard::getAnswer()
{
大小\u t pos=0;
pos=fileLine.find(“=”)+1;
应答=文件行.substr(pos);
返回答案;
}
//以整数形式返回答案。
int getAnswerValue()
{
返回答案;
}
2.cpp
#包括
#包括
#包括
#包括
#包括
#包括“arithcard.hpp”
//文件本地匿名命名空间
名称空间{
int-verbosity=1;
}
/*
读入卡片的文件,然后循环:查询用户以指定
提问,打印问题,检查答案。当用户
键入空行,而不是指定问题。
*/
int main(int argc,const char*argv[]
{
如果(argc!=2){
std::cout来自§21.4.2/1[字符串要求]

如果任何操作将导致
size()
超过
max\u size()
,则该操作将抛出类型为
length\u error
的异常对象

因此,在代码的某个地方,您试图创建一个长度超过的字符串,这通常是一个巨大的数字,因此您应该为自己感到自豪:-)

撇开玩笑不谈,很难判断错误在哪里,因为看起来您删除了相关的代码部分,特别是
ArithCard::readCards
的实现。我不明白为什么您选择将此函数设置为
静态
,然后将指向
ArithCard
实例的指针传递给它,而不仅仅是使其成为非静态成员函数

您的代码将要传递给
ArithCard::readCards
的指针初始化为
nullptr
,因此我假设您正在为函数中的对象分配内存。如果没有,那么您很可能在该函数中有未定义的行为

我会把这个函数改为

class ArithCard {
  public:
  ...
  bool readCards(const std::string &fileName, int &cardCnt) ;
};
并将
main()
中的代码更改为

ArithCard cards;

result = cards.readCards(dataFile, cardCnt);

另外,在
getQuestion()和
getAnswer()中
您没有检查
string::find
的返回值,因此尝试使用无效结果提取子字符串可能是导致此错误的原因。这是最有可能的解释,因为如果找不到搜索项,
string::find
将返回一个巨大的数字。

我不想更改main()或者函数标题。你所建议的可能就是我出错的原因。我将尝试看看为什么它找不到我的字符串值。
#include <iostream>
#include <sstream>
#include <string>
#include <limits>
#include <random>
#include "arithcard.hpp"

// File-local anonymous namespace

namespace {

int verbosity = 1 ;

}

/*
  Read in the files of cards, then loop: query the user to specify a
  question, print the question, check the answer. Terminate when the user
  types an empty line instead of specifying a question.
*/
int main (int argc, const char *argv[])
{
  if (argc != 2) {
    std::cout << "Usage: " << argv[0] << " <datafile>" << std::endl ;
    return (1) ;
  }
  std::string dataFile(argv[1]) ;

  bool result = false ;
  int cardCnt = -1 ;
  ArithCard *cards = nullptr ;

/*
  Parse the file of questions & answers. A false return value indicates an
  absent file or other i/o error. It's also possible the file is present but
  contains no valid questions.
*/
result = ArithCard::readCards(dataFile,cardCnt,cards) ;
std::cout << cards[0].getQuestion() << std:: endl;

  return (0) ;
}
class ArithCard {
  public:
  ...
  bool readCards(const std::string &fileName, int &cardCnt) ;
};
ArithCard cards;

result = cards.readCards(dataFile, cardCnt);