473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

template class syntax


Hi,

I have trouble to compile the following piece of code with g++3.4 but not
with earlier version

// Foo.h
template<typename T>
class Foo
{
public:
void f();
};

// Foo.cpp
#include <iostream>
#include "Foo.h"

template<typename T>
void Foo<T>::f()
{
std::cout << "Foo<T>::f()\n";
}

template Foo<int>;
// main.cpp
#include "Foo.h"

int main()
{
Foo<int> x;
x.f();
}
The error message when using g++3.4 is

Foo.cpp:10: error: expected unqualified-id before ';' token

i.e. the template command "template Foo<int>;"

Is this syntax not standard?

Cheers,

Patrick

Jul 23 '05 #1
3 8220
Patrick Guio wrote:
i.e. the template command "template Foo<int>;"
In C++ there are no commands. There are, however, statements.
Is this syntax not standard?


No, it is not. 'template class Foo<int>;' is.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #2
Patrick Guio wrote:
Hi,

I have trouble to compile the following piece of code with g++3.4 but not with earlier version

// Foo.h
template<typename T>
class Foo
{
public:
void f();
};

// Foo.cpp
#include <iostream>
#include "Foo.h"

template<typename T>
void Foo<T>::f()
{
std::cout << "Foo<T>::f()\n";
}

template Foo<int>;
// main.cpp
#include "Foo.h"

int main()
{
Foo<int> x;
x.f();
}
The error message when using g++3.4 is

Foo.cpp:10: error: expected unqualified-id before ';' token

i.e. the template command "template Foo<int>;"

Is this syntax not standard?

Cheers,

Patrick

Patrick, I believe that you need to move the definition of you f()
function into the header file. Templates need to have their member
function defined in the header where the class is defined or you will
recieve a linker error (If I recall correctly). So move what is in
foo.cpp into foo.h. Also, I am not sure what you are trying to
accomplish with the line "template Foo<int>;"... Hope this will help
you out some.

- ZT

Jul 23 '05 #3
zeotherm wrote:
Patrick, I believe that you need to move the definition of you f()
function into the header file. Templates need to have their member
function defined in the header where the class is defined or you will
recieve a linker error (If I recall correctly).
You recall only half correctly: with most compilers you need to put
template definitions into header files if you use implicit
instantiation. However, he actually tries to do explicit instantiation
which removes the need of having the template definition in the header.
So move what is in foo.cpp into foo.h.
A better advice is: use the correct syntax for explicit instantiation!
Also, I am not sure what you are trying to
accomplish with the line "template Foo<int>;"...


This look much like an explicit instantiation but is not quite. The
correct syntax is this:

template class Foo<int>;

This instantiates all member of the class template 'Foo' with the
template parameter 'int'. After this instantiation, you can use
'Foo<int>' in all translation units without putting the definitions
of the member functions into a header. Of course, if you need 'Foo'
for other types, too, you either need to add appropriate
instantiations or make the definitions available for implicit
instantiation.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rennie deGraaf | last post by:
The attached code compiles and works properly when I comment out the declaration, definition, and invocations of the method 'eck'. With "eck" in there, g++ fails with ttest.cpp:23: template-id...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
5
by: Steve | last post by:
Hi, Does C++ allow the programmer to declare a template with in a template so that a generic function can instantiate the embedded template? For example, could code such as this exist: ...
6
by: paul | last post by:
When I compile class B shown below, in VC Express 2005, I get error C2061: syntax error : identifier '{ctor}. Would appreciate any info on how to avoid the error. ref class X{ X(); ~X(); }; ...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
4
by: Howard Gardner | last post by:
// I think that there is no way to write the template "cant_write_me" in // this example. Would love to be wrong. // one of many approaches that won't work template< template< typename class...
2
by: Gary Nastrasio | last post by:
I'm currently reading Andrei Alexandrescu's book "Modern C++ Design" and I'm a bit confused by one bit of template syntax in chapter 1. Here is a code example: template <class CreationPolicy>...
2
by: Andy Champ | last post by:
We have a class with something that, simplified, looks like this: template <typename Tclass foo { T beyondAllReaching; // In VC2005, this works OK template <typename Ufriend void...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.