By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

compilation error with templates

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ma***********@gmail.com wrote:
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
Jul 23 '05 #2

P: n/a
> 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>::other
long_allocator;
}

...to give an impractical example.

Jul 23 '05 #3

P: n/a
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<X<float> >. Why? Because it doesn't know if std::vector<X<float>::iterator is a type name or a member variable.


Regards,
Ben
Jul 23 '05 #4

P: n/a
It works !!

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

Marc

Jul 25 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.