0
votes

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)"

1
I don't know why the <> aren't showing up in the code, but the declaration is: template<typename T> vector<T> intersect(const vector<T> & v1, const vector<T> & v2) and the variable vector<T> resultVector;Rick
Please copy-paste the complete error output into the body of the question, use code-formatting so all characters will be shown. Also please point out (with e.g. comments) where in the code the errors are. And if possible please try to create a Minimal, Complete, and Verifiable Example that you can show us.Some programmer dude
At least show all the includes and "using namespace" things you have. Or better, remove all the using directives and prefix the relevant names with the appropriate qualifier.Mat
Not much to go on here, but have you done #include <vector> before your template method?Max Value

1 Answers

1
votes

You should not do using namespace std in any header files. You should add the explicitly std:: namespace to your template implementation, and the problem should go away (assuming you have included the appropriate standard library headers).