compilation error with templates

M

Marc.Tajchman

Hi everybody,

Here is a simple C++ example :

1 #include <vector>
2
3 template <typename T>
4 class X {
5 T _t;
6 public :
7 X(const T & t) : _t(t) {}
8 };
9
10 template <typename U >
11 void f(U t)
12 {
13 std::vector< X<U> > v;
14 std::vector< X<U> >::iterator iv;
15 }
16
17 int main()
18 {
19 std::vector<int>::iterator i;
20 std::vector< X<float> >::iterator j;
21 return 0;
22 }

Line 14 gives me the following compiler error (g++ 3.4.1) :
----------------------------------------------------------
test.cxx: In function `void f(U)':
test.cxx:14: error: expected `;' before "iv"
----------------------------------------------------------
but line 20 is accepted.

Could you tell me what's wrong (probably a simple syntax problem,
but I don't see which one) ?

Thanks in advance.

Marc
 
V

Victor Bazarov

Hi everybody,

Here is a simple C++ example :

1 #include <vector>
2
3 template <typename T>
4 class X {
5 T _t;
6 public :
7 X(const T & t) : _t(t) {}
8 };
9
10 template <typename U >
11 void f(U t)
12 {
13 std::vector< X<U> > v;
14 std::vector< X<U> >::iterator iv;
15 }
16
17 int main()
18 {
19 std::vector<int>::iterator i;
20 std::vector< X<float> >::iterator j;
21 return 0;
22 }

Line 14 gives me the following compiler error (g++ 3.4.1) :
----------------------------------------------------------
test.cxx: In function `void f(U)':
test.cxx:14: error: expected `;' before "iv"
----------------------------------------------------------
but line 20 is accepted.

Could you tell me what's wrong (probably a simple syntax problem,
but I don't see which one) ?

Read about "dependent name" in the FAQ.

And in the future posts, please don't put numbers next to every line,
put the number of a line of interest in a comment in the same line.

V
 
A

Alipha

std::vector< X<U> >::iterator iv;

Here, std::vector can't be instantiated until f is used and U is known.
Since the std::vector template isn't instantiated, the compiler can't
know anything about the members of the std::vector class (namely
because of template specialization,) and so, is iterator a typename or
a static member? the compiler can't know and assumes the latter. you
need to tell it otherwise:

typename std::vector< X<U> >::iterator iv;

the template keyword can also be used in contexts such as these to
relieve ambiguity:

template<class U>
void f() {
typename std::vector<U>::allocator_type::template rebind<long>::eek:ther
long_allocator;
}

...to give an impractical example.
 
B

benben

Hi everybody,

Here is a simple C++ example :

1 #include <vector>
2
3 template <typename T>
4 class X {
5 T _t;
6 public :
7 X(const T & t) : _t(t) {}
8 };
9
10 template <typename U >
11 void f(U t)
12 {
13 std::vector< X<U> > v;
14 std::vector< X<U> >::iterator iv;
15 }
16
17 int main()
18 {
19 std::vector<int>::iterator i;
20 std::vector< X<float> >::iterator j;
21 return 0;
22 }

Line 14 gives me the following compiler error (g++ 3.4.1) :
----------------------------------------------------------
test.cxx: In function `void f(U)':
test.cxx:14: error: expected `;' before "iv"
----------------------------------------------------------
but line 20 is accepted.

Could you tell me what's wrong (probably a simple syntax problem,
but I don't see which one) ?

Thanks in advance.

Marc

1. Please don't add line numbers. It is hard to copy your code and give a
quick compilation because we have to manually delete those line numbers.
Simply comment at the line that's causing problems.

2. Visual C++ compiled the code fine. This could be an accidant.

3. g++ compiled the code fine if you add a keywork 'typename' before
std::vector said:
::iterator is a type name or a member variable.

Regards,
Ben
 
M

Marc.Tajchman

It works !!

Thanks very much for all the answers and sorry for putting
line numbers at an inconvenient posision.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top