【发布时间】:2011-11-04 02:34:53
【问题描述】:
我正在尝试将我的程序组织成函数并遇到了这个问题,
错误:“'.' 之前缺少模板参数令牌”
一旦我尝试在函数中运行代码,如果它只是在 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是什么?是class吗? -
是的,它是一个类,orderedList(numFilm)是一个构造函数
-
这段代码似乎没有任何问题。您可能想为
class orderlist提供minimal 代码。 -
添加了一些相关代码,有帮助吗?
-
您还没有准备好使用模板。回去研究一下类和类的实例之间的区别。
标签: c++ templates compiler-errors