Reputation: 2458
I am trying to organize my program into functions and have ran into this,
error: "missing template arguments before '.' token"
once I try to run the code in the function, it works fine if its just in main()
. Anyone familiar with this error know what the issue may be?
Note, the commented out code removes the error but messes with the ordered list class
and resets its length or something, causing the orderedlist.getlength()
function to return 0
, which makes none of the code in the while()
loop execute.
function:
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");
}
}
}
}
Insert in orderedList class (where length is determined):
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;
}
code in main where list is filled:
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);
}
Please let me know if code from anywhere else in the program would be helpful in assessing this error.
Upvotes: 4
Views: 21385
Reputation: 69988
It seems that you populate a variable orderedList
in main()
and then expect it to be automatically available in rentFilm(...)
when you declare with the same name; that is not possible. You have to pass the object to the function from main()
or better to make that function as member method of the class orderedList
:
int main ()
{
orderedList<filmType> ol(numFilm); // variable name different (good practice)
... // all the populating
orderedList.rentFilm(...); // call the function like this
}
where, rentFilem()
is now part of the class
class orderedList {
...
public:
void rentFilm(char* filmId, char* custId, char* rentDate, char* dueDate, int numFilm);
};
Now inside the function, you don't have to declare variable for orderedList
; just use this-><method/variable>
. It should work.
Upvotes: 0
Reputation: 7550
Is the problem that you are creating a variable with the same name as the template? When you say,
orderedList<filmType> orderedList(numFilm);
that's (sort of) like saying,
int int=42;
and then expecting int+1
to return 43
Try something like,
orderedList<filmType> ol(numFilm);
And changin all the other references to orderedList
, to ol
.
Upvotes: 0
Reputation: 8471
It looks like the line you commented out declares a variable with the same name as a class.
So when you comment it out, static functions of that class are getting invoked.
Change the declaration to something like:
orderedList<filmType> filmList(numFilm);
and then change all the references of orderedList
in the function to filmList
.
Upvotes: 1