0

I'm trying my very first template. the following code compiles :

template<class T,class C=int> class MyClass
{};

But not this :

#include <vector>

using namespace std;
template<class T,class C=vector<T>> class MyClass
{};

Yet i see the standard vector class template declared like this :

template < class T, class Alloc=allocator<T> > class vector
{};

The errors the compiler throws are :

*error: spurious '>>', use '>' to terminate a template argument list
*error: definition of 'class MyClass' inside template parameter list
*error: two or more data types in declaration of 'type name'
*error: expected '>' before ';' token
*error: expected unqualified-id before ';' token

3
  • Perhaps you'd get use out of a compiler with better error messages. error: a space is required between consecutive right angle brackets (use '> >') I also assume you knew about this one, but error: no template named 'vector'; did you mean 'std::vector'? Commented Jul 11, 2015 at 23:48
  • 1
    You might want to add std:: before vector. Commented Jul 11, 2015 at 23:49
  • 1
    If you compile it as C++11, you should be able to use >> without the space. Commented Jul 11, 2015 at 23:51

2 Answers 2

2

http://coliru.stacked-crooked.com/a/f93734d989e10446

No, it works. You just forgot the std namespace.

Sign up to request clarification or add additional context in comments.

5 Comments

@coucou8949 Don't do this. This causes headaches if library names collide.
@coucou8949 Yes. You should use std::.
i removed this "using namespace std;" and did as you said.still the same errors.
The linked site compiles it as C++14. If you're compiling as regular old C++98, you must write std::vector<T> > instead std::vector<T>>.
error: a space is required between consecutive right angle brackets (use '> >'). i think that's the problem.that's what the compiler should have thrown.someone should post a proper answer so i can make it the correct one.
1

the code was :

#include <vector>

using namespace std;
template<class T,class C=vector<T>> class MyClass
{};

but i should have written :

#include <vector>

template<class T,class C=std::vector<T> > class MyClass//with space between right angle brackets  : '> >'
{};

1 Comment

The requirement for the space was removed in C++11 - consider using a more up-to-date compiler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.