473,503 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compilation error with templates

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
4 1363
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
> 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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1548
by: Ajay Daptardar | last post by:
Hi, I have the following problem. Consider this: // codec.h template <class T> class codec { public: codec(T val); private:
2
1496
by: Connell Gauld | last post by:
Hi, I am having problems compiling a small project in VC++ 6. I have included the error below and the full project (including workspace) can be downloaded here:...
12
12833
by: blueblueblue2005 | last post by:
Hi, here is an example I copied from Deitel C++ book. but when I compile it, always get the above compilation error, no matter how I change the include order, please help. here is the files:...
5
5841
by: Mikael S. H. | last post by:
Header file compilation I'm coding a small irc bot, and I've noticed that compilation takes very long when I add certain header files (compared to compilation time without). I've tried to find...
4
2717
by: Marcelo | last post by:
Hi everybody, This is my first time with the template class and I have an strange problem. I have spent all the afternoon trying to understand it, but I don't get the problem... I have three...
8
1763
by: vitalyt | last post by:
Hi, I have cpp file which consist nested templates. Compilation time without optimization is 1-2 minutes, with -O2 turned on more then 5 hours. :( Could anybody help with that problem? AIX 5.2,...
9
2650
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
7
184
by: Christof Warlich | last post by:
Hi, can anyone tell why line 6 below gives the compilation error: error: expected primary-expression before '>' token Everything works fine if MemoryBlockSizes is replaced by a constant, e.g....
1
2643
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC:...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3156
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.