我仍然是编程c ++的新手 - 所以请原谅我,如果我的代码还不完善(仍然可以提出改进建议;)
我在运行此代码时遇到问题(只是大型项目的一部分)。
int stock::import(string file){
ifstream input;
input.open(file.c_str());
input.ignore(100000, '\n');
while(!input.eof()){
int y = 0;
int m = 0;
int d = 0;
string year;
string month;
string day;
string open;
string high;
string low;
string close;
string volume;
string adj;
{
getline(input, year, '-');
y = atoi(year.c_str());
}
{
getline(input, month, '-');
m = atoi(month.c_str());
}
{
getline(input, day, ',');
d = atoi(day.c_str());
}
int index = findplace(y, m, d);
values[index].year = y;
values[index].month = m;
values[index].day = d;
{
getline(input, open, ',');
values[index].open = open;
}
{
getline(input, high, ',');
values[index].high = high;
}
{
getline(input, low, ',');
values[index].low = low;
}
{
getline(input, close, ',');
values[index].close = close;
}
{
getline(input, volume, ',');
values[index].volume = volume;
}
{
getline(input, adj, '\n');
values[index].adj = adj;
}
}
return 0;}
项目编译正常但是当调用函数“import”时,我的程序立即崩溃。调试器在这部分代码中给我回复了“无法找到当前函数的绑定”的错误:
{
getline(input, open, ',');
values[index].open = open;
}
奇怪的是,如果我删除这部分代码:
{
getline(input, year, '-');
y = atoi(year.c_str());
}
几行代表我的程序工作正常的关键部分(意味着它不会崩溃,但当然不会做它应该做的事情)。
我目前正在将CodeBlocks作为IDE取消。
任何人都可以帮我吗?我现在非常绝望,因为我已经尝试了所有我知道的事情......
如此处所示,有关值和findplace()的更多信息:
class stock{
private:
string name = "";
string code = "";
string number = "";
stock* next = NULL;
int findplace(int year, int month, int day);
public:
day* values = new day[30];
stock(string c);
string getindex();
void setnext(stock* n);
stock* getnext();
int import(string file);
};
所以基本上值[]只是一个指向“day”类对象的指针数组 class“day”看起来像这样:
class day{
public:
int year = 0;
int month = 0;
int day = 0;
string open;
string high;
string low;
string close;
string volume;
string adj;
};
并且在函数findplace()中我厌倦了在数组中插入新的一天,以便数组按日期排序并始终保持最新的日期并在插入新的日期时丢弃最旧的日期。它看起来像这样:
int stock::findplace(int year, int month, int day){
int j = 29;
for(j; values[j].year > year; j--){
}
for(j; values[j].month > month; j--){
}
for(j; values[j].day > day; j--){
}
if(!(values[j].year == year && values[j].month == month && values[j].day == day)){
delete &values[0];
for(int i = 0; i < j; i++){
values[i] = values[i+1];
}
}
return j;
}
答案 0 :(得分:0)
尝试打印年/月月/日/ d的值。 我会假设您不知何故从输入文件中错误地读取了这些值,并且正在为您的values数组生成一个越界索引。