我尝试使用链表打开一个测试文件。部分代码是这样的
#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()
错误说明 what(): basic_string::at 任何想法?