473,549 Members | 2,573 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 1366
Ma***********@g mail.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>::o ther
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<f loat> >. Why? Because it doesn't know if std::vector<X<f loat>::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
1557
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
1503
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: http://www.freebreakfast.co.uk/Example1.zip I am new to C++ (esp. using multiple files) and I am not sure I have structured the files right (with the correct includes etc). Any other...
12
12843
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: Note: this is to practice Proxy classes. // Implementation.h class Implementation {
5
5845
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 out if it is possible to compile a header file, yet I have only found that it is possible, but not how it is done. I have RTFM, STFW and searched...
4
2720
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 files: matrix.cpp (template class) matrix.h test.cpp
8
1767
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, Visual Age C++ 6 compiler #include <pair> using namespace std; // value typedef
9
2654
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; virtual valueType Value() = 0; };
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. an int. Thanks for any hint,
1
2649
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: Sun C++ 5.9 SunOS_sparc Patch 124863-01 2007/07/25
0
7459
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7967
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5377
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5097
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1953
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 we have to send another system
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
772
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.