I wrote a short template method for a school project, and am getting weird syntax errors that I don't understand. They have something to do with the template I declared (when commenting out the header containing the template, all errors in main() go away) but I can't figure out what is going on. I'm including and "intersectHeader.h" and using the std namespace. In my main() function, all I'm doing is declaring a vector:
vector v1; -- errors: C2065 'string' undeclared identifier, C2065 'v1' undeclared identifier, C2065 'vector' undeclared identifier
This is the template:
template<typename T>
vector<T> intersect(const vector<T> & v1, const vector<T> & v2)
{
vector<T> resultVector;
bool duplicate = false;
for (int i = 0; i < v1.size(); i++)
{
duplicate = false;
for (int j = 0; j < v2.size(); j++)
{
if (v1[i] == v2[j])
{
for (int a = 0; a < resultVector.size(); a++)
{
if (v1[i] == resultVector[a])
{
duplicate = true;
break;
}
}
if (!duplicate)
{
resultVector.push_back(v1[i]);
}
}
}
}
return resultVector;
}
The above gives me these errors:
C2988 unrecognizable template declaration/definition, C2143 syntax error missing ';' before '<', C2059 syntax error '<'.
All of which occur in the second line "vector intersect(const vector & v1, const vector & v2)"
#include <vector>
before your template method? – Max Value