gpt4 book ai didi

C++ 'std::out_of_range'

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:13 28 4
gpt4 key购买 nike

<分区>

我正在尝试使用链表打开测试文件。部分代码是这样的

#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <list>
#include <iomanip>
#include <stdexcept>
using namespace std;

class isbn
{
private:
string code;
int digit;

public:
//constructor
isbn(): code(""), digit(0) { }

//copy constructor
isbn(const isbn &other):
code(other.code),
digit(other.digit)
{
for(unsigned int i=0; i < (unsigned int) digit; i++)
code[i] = other.code[i];
}
void setCode(const char &temp);
void setDigit(const int &num);

isbn operator = (const isbn &other)
{
code = other.digit;
digit = other.digit;
for(unsigned int i=0; i < (unsigned int) digit; i++)
code[i] = other.code[i];
return *this;
}

};

void isbn::setCode(const char &temp)
{
code = temp;
}
void isbn::setDigit(const int &num)
{
digit = num;
}

void extIsbn_in_file(list<isbn> &isbns, const string &filename)
{
ifstream filein;
filein.clear();
filein.open(filename.c_str());
if(!filein)
{
cout << "error \n";
exit(0);
}
cout << "\n";

string contents;
isbn aisbn;
list<isbn>::iterator isbnitr;
isbnitr = isbns.begin();
int count = 0;

while(!filein.eof())
{
getline(filein, contents, '\n');
aisbn.setCode(contents.at(count));
aisbn.setDigit(count);
isbns.push_back(aisbn);
count++;

..<more code>......
..<more code>......

}
filein.close();
}

int main(int argc, char *argv[])
{
if(argc > 0)
{
if(argc != 2)
{
cout << "invalid number of argument!! \n";
exit(0);
}
list<isbn> code;
extIsbn_in_file(code, argv[1]);
}
else
{
cout << "invalid number of argument!! \n";
exit(0);
}
return 0;
}

调用给定行时肯定会出现问题

        aisbn.setCode(contents.at(count));

在这段代码中,调用了 at 方法,但不太确定我做错了 setCode() 还是尝试使用 at()

错误说 什么(): basic_string::at有什么想法吗?

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