我试图将我的程序组织成函数并遇到了这个问题,
一旦我尝试在函数中运行代码,如果它只是在 main()
中,它就可以正常工作。任何熟悉此错误的人都知道可能是什么问题吗?
请注意,注释掉的代码会删除错误,但会混淆有序列表 class
并重置其长度或其他内容,导致 orderedlist.getlength()
函数变为 return 0
,这使得 while()
循环中的任何代码都不会执行。
函数 :
void rentFilm(char* filmId, char* custId, char* rentDate, char* dueDate, int numFilm)
{
//orderedList <filmType> orderedList(numFilm);
//filmType newItem;
int index = 0;
bool found = false;
while (index < orderedList.getLength() && !found)
{
cout << "test" << endl;
if (strncmp(filmId,orderedList.getAt(index).number,6) == 0 && strncmp("0000",orderedList.getAt(index).rent_id,5) == 0)//If that film is rented by NO customer
{
cout << "test" << endl;
found = true;//customer can rent it
strcpy(newItem.number,filmId);
orderedList.retrieve(newItem);
orderedList.remove(newItem);
strcpy(newItem.rent_id,custId);
strcpy(newItem.rent_date,rentDate);
strcpy(newItem.return_date,dueDate);
orderedList.insert(newItem);
cout << "Rent confirmed!" << endl;
}
else
{
if (strncmp(filmId,orderedList.getAt(index).number,6) > 0 || strncmp("0000",orderedList.getAt(index).rent_id,5) > 0)
{
++ index;
}
else
{
throw string ("Not in list");
}
}
}
}
在orderedList 类中插入(长度已确定) :
template <class elemType>
void orderedList<elemType>::insert(const elemType& newItem)
{
int index = length - 1;
bool found = false;
if (length == MAX_LIST)
throw string ("List full - no insertion");
// index of rear is current value of length
while (! found && index >= 0)
if (newItem < list[index])
{
list[index + 1] = list [index]; // move item down
--index;
}
else
found = true;
list [index + 1] = newItem; // insert new item
++length;
}
main 中的 代码,其中填充了列表:
filmFile.open("films.txt", ios::in);
filmFile >> numFilm;
filmFile.get();
orderedList <filmType> orderedList(numFilm);
filmType newItem;
readString(filmFile, newItem.number,5);
for (int i = 0; i < numFilm; i++)
{
newItem.copy = filmFile.get();
readString(filmFile, newItem.title,30);
readString(filmFile, newItem.rent_id,4);
readString(filmFile, newItem.rent_date,8);
readString(filmFile, newItem.return_date,8);
filmFile.get();
orderedList.insert (newItem);//puts filmType struct into the ordered list.
readString(filmFile, newItem.number,5);
}
请让我知道程序中其他任何地方的代码是否有助于评估此错误。
最佳答案
看起来您注释掉的行声明了一个与类同名的变量。
因此,当您将其注释掉时,会调用该类的静态函数。
将声明更改为:
orderedList<filmType> filmList(numFilm);
然后将函数中所有对
orderedList
的引用改为 filmList
。关于c++ - '.' 标记前缺少模板参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8004303/