Ross Youngblood
Ross Youngblood

Reputation: 536

Strange code throwing compile error template< J, int aSize=10> C2143: syntax error : missing ';' before '<'

I'm working on an interesting problem.

Customer has code that builds in VS2013, they had a proliferation of macros, and in one case I couldn't understand why. Visual Studio 2013 Platform toolset V120xp.

template< class J, int a_iSize = 10 >
struct testme {
private:
    vector< J > data;
};

The above code throws the error:

1>c:\customers\xxx.h(334): error C2143: syntax error : missing ';' before '<'

However, if they change all of their USES of the struct testme by wrapping it with a macro as in:

#define TEST_ME testme

The code compiles fine.

I'm a newbie to templates and wonder if there is something base level that I'm missing.

I generally understand the concept of templates, but this is an interesting nuance.

I need to dig down and create a smaller test case. At the moment, the above code snippet is part of a larger block of code, so there could be something in the call that is causing this.

Also, I could run the 'C' preprocesor only and compare the two *.i files. This may show up something.

The customer wrapping the use cases of the template function with a macro is interesting. When I see this, I think "There is something missing, that if done, could eliminate the use of a macro." That's the thing I'm looking for.

Upvotes: 0

Views: 41

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35454

For this code:

template< class J, int a_iSize = 10 >
struct testme {
private:
    vector< J > data;
};

int main()
{
    testme<int> t;
}

The following error:

error C2143: syntax error : missing ';' before '<

is duplicated here.

What is probably occurring is the lack of specifying that vector is in the std namespace.

This is why header files should use the namespace name, and not rely on some outside file to provide this name.

In addition, the <vector> header should be included in the file.

#include <vector> 
template< class J, int a_iSize = 10 >
struct testme {
private:
    std::vector< J > data;
};

int main()
{
    testme<int> t;
}

The following compiles with no errors.

Upvotes: 2

Related Questions