Error on the denoted line below. What gives?
template <class T> T List<T>::count(T thisElement) {
node* curNodePtr = firstNodePtr;
int cnt = 0;
while (curNodePtr) {
if (curNodePtr->val == T) // error: expected primary-expression before ')'
++cnt;
curNodePtr = curNodePtr->next;
}
return cnt;
}
template <class T> T List<T>::count(T thisElement) {
First of all, if you write a count method, I guess, that you rather want to return an int
rather than T
, so this line should look like this:
template <class T> int List<T>::count(T thisElement) {
Let's go on...
if (curNodePtr->val == T)
I'm sure you wanted to write something like:
if (curNodePtr->val == thisElement)
T
is a type. If you later specialize your class with, say, int
, your code line would become:
if (curNodePtr->val == int)
This is why compiler complains.
Why am I getting a “error: expected primary-expression before ')' token”?
Because T
is a type. You need to compare to an instance. For example, T()
.
if (curNodePtr->val == T()) ....
^^
Besides that, you have to make sure your function actually does something sensible. That is another matter.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.