473,564 Members | 2,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems compiling templates with ubuntu c++

Hello all. I am a newbie following the C++ tutorial in :
http://www.cplusplus.com/doc/tutorial/templates.html
I am unable to succesfully compile any of the examples with templates
of this tutorial. I use the standard c++ compiler which comes with
ubuntu breezy distro. I guess that somethig is wrong with it or (more
likely..) I should use some option when compiling. This is, for
instance, one of the codes:

// template specialization
#include <iostream>
using namespace std;

template <class T>
class container {
T element;
public:
container (T arg) {element=arg;}
T increase () {return ++element;}
};

template <>
class container <char{
char element;
public:
container (T arg) {element=arg;}
char uppercase ();
};

template <>
char container<char> ::uppercase()
{
if ((element>='a') &&(element<='z' ))
element+='A'-'a';
return element;
}

int main () {
container<intmy int (7);
container<charm ychar ('j');
cout << myint.increase( ) << endl;
cout << mychar.uppercas e() << endl;
return 0;
}
and these are the error messages (sorry, some of them are in spanish..)

templates3.c:17 : error: expected `)' before 'arg'
templates3.c:22 : error: el id de plantilla 'uppercase<>' para
'char container<char> ::uppercase()' no coincide con ninguna
declaración de plantilla
templates3.c:22 : error: declaración inválida de función
templates3.c: In function 'int main()':
templates3.c:31 : error: no se encuentra una función coincidente para
la llamada a 'container<char >::container(ch ar)'
templates3.c:14 : nota: los candidatos son: container<char> ::container()
templates3.c:14 : nota:
container<char> ::container(con st container<char> &)
Can anyone tell me what is going on. Thanks in advance.

Sep 10 '06 #1
6 2587
In article <11************ **********@m79g 2000cwm.googleg roups.com>,
jo**********@gm ail.com says...
Hello all. I am a newbie following the C++ tutorial in :
http://www.cplusplus.com/doc/tutorial/templates.html
I am unable to succesfully compile any of the examples with templates
of this tutorial. I use the standard c++ compiler which comes with
ubuntu breezy distro. I guess that somethig is wrong with it or (more
likely..) I should use some option when compiling. This is, for
instance, one of the codes:
It looks to me like the problem is with your code.
// template specialization
#include <iostream>
using namespace std;

template <class T>
class container {
T element;
public:
container (T arg) {element=arg;}
T increase () {return ++element;}
};
Here "T" has a meaning because it's the name of a template argument.
template <>
class container <char{
char element;
public:
container (T arg) {element=arg;}
Here, the compiler is complaining because "T" does NOT have any defined
meaning. Since you're specializing for char, it appears this should
probably be:

container (char arg) : element(arg) {}
template <>
char container<char> ::uppercase()
{
if ((element>='a') &&(element<='z' ))
element+='A'-'a';
return element;
This, should compile but isn't really portable. It would be better to
use ::toupper or to toupper from some locale. In some character sets,
there are characters between 'a' and 'z' that are not letters (EBCDIC
being the most obvious).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Sep 10 '06 #2
Josefo wrote:
Hello all. I am a newbie following the C++ tutorial in :
http://www.cplusplus.com/doc/tutorial/templates.html
I am unable to succesfully compile any of the examples with templates
of this tutorial. I use the standard c++ compiler which comes with
ubuntu breezy distro. I guess that somethig is wrong with it or (more
likely..) I should use some option when compiling.
Nope, the code from that web-site is broken.
This is, for instance, one of the codes:

// template specialization
#include <iostream>
using namespace std;

template <class T>
class container {
T element;
public:
container (T arg) {element=arg;}
T increase () {return ++element;}
};

template <>
class container <char{
char element;
public:
container (T arg) {element=arg;}
T is not known here. Make that:

container ( char arg ) { element = arg; }
char uppercase ();
};

template <>
Ditch the line above. You are just defining a member that has already been
declared.
char container<char> ::uppercase()
{
if ((element>='a') &&(element<='z' ))
element+='A'-'a';
return element;
}

int main () {
container<intmy int (7);
container<charm ychar ('j');
cout << myint.increase( ) << endl;
cout << mychar.uppercas e() << endl;
return 0;
}
and these are the error messages (sorry, some of them are in spanish..)

templates3.c:17 : error: expected `)' before 'arg'
templates3.c:22 : error: el id de plantilla 'uppercase<>' para
'char container<char> ::uppercase()' no coincide con ninguna
declaración de plantilla
templates3.c:22 : error: declaración inválida de función
templates3.c: In function 'int main()':
templates3.c:31 : error: no se encuentra una función coincidente para
la llamada a 'container<char >::container(ch ar)'
templates3.c:14 : nota: los candidatos son: container<char> ::container()
templates3.c:14 : nota:
container<char> ::container(con st container<char> &)
Can anyone tell me what is going on. Thanks in advance.

Best

Kai-Uwe Bux
Sep 10 '06 #3
Thanks, Jerry and Kai-Uwe. Of course, you were right!. Now it works
fine. Best regards

Josefo

Kai-Uwe Bux wrote:
Josefo wrote:
Hello all. I am a newbie following the C++ tutorial in :
http://www.cplusplus.com/doc/tutorial/templates.html
I am unable to succesfully compile any of the examples with templates
of this tutorial. I use the standard c++ compiler which comes with
ubuntu breezy distro. I guess that somethig is wrong with it or (more
likely..) I should use some option when compiling.

Nope, the code from that web-site is broken.
This is, for instance, one of the codes:

// template specialization
#include <iostream>
using namespace std;

template <class T>
class container {
T element;
public:
container (T arg) {element=arg;}
T increase () {return ++element;}
};

template <>
class container <char{
char element;
public:
container (T arg) {element=arg;}

T is not known here. Make that:

container ( char arg ) { element = arg; }
char uppercase ();
};

template <>

Ditch the line above. You are just defining a member that has already been
declared.
char container<char> ::uppercase()
{
if ((element>='a') &&(element<='z' ))
element+='A'-'a';
return element;
}

int main () {
container<intmy int (7);
container<charm ychar ('j');
cout << myint.increase( ) << endl;
cout << mychar.uppercas e() << endl;
return 0;
}
and these are the error messages (sorry, some of them are in spanish..)

templates3.c:17 : error: expected `)' before 'arg'
templates3.c:22 : error: el id de plantilla 'uppercase<>' para
'char container<char> ::uppercase()' no coincide con ninguna
declaración de plantilla
templates3.c:22 : error: declaración inválida de función
templates3.c: In function 'int main()':
templates3.c:31 : error: no se encuentra una función coincidente para
la llamada a 'container<char >::container(ch ar)'
templates3.c:14 : nota: los candidatos son: container<char> ::container()
templates3.c:14 : nota:
container<char> ::container(con st container<char> &)
Can anyone tell me what is going on. Thanks in advance.

Best

Kai-Uwe Bux
Sep 12 '06 #4
Hello again (I am sorry...), but after spending a lot of time looking
to this easier example, I cannot find the error:
// class templates
#include <iostream>
using namespace std;
template <class T>
class pair {
T a, b;
public:
pair (T first, T second)
{a=first; b=second;}
T getmax ();
};
template <class T>
T pair <T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}

int main () {
pair <intmyobject (100, 75);
cout << myobject.getmax ();
return 0;
}
When compiling, I get:

josemanuel@jose fo:~/C++$ c++ templates2.c
templates2.c:13 : error: expected initializer before '<' token
I simply does not understand where is he error. Everything looks OK in
this line. After trying several "fenomenologina l" modifications I give
up. Some hint?

Josefo wrote:
Thanks, Jerry and Kai-Uwe. Of course, you were right!. Now it works
fine. Best regards

Josefo

Kai-Uwe Bux wrote:
Josefo wrote:
Hello all. I am a newbie following the C++ tutorial in :
http://www.cplusplus.com/doc/tutorial/templates.html
I am unable to succesfully compile any of the examples with templates
of this tutorial. I use the standard c++ compiler which comes with
ubuntu breezy distro. I guess that somethig is wrong with it or (more
likely..) I should use some option when compiling.
Nope, the code from that web-site is broken.
This is, for instance, one of the codes:
>
// template specialization
#include <iostream>
using namespace std;
>
template <class T>
class container {
T element;
public:
container (T arg) {element=arg;}
T increase () {return ++element;}
};
>
template <>
class container <char{
char element;
public:
container (T arg) {element=arg;}
T is not known here. Make that:

container ( char arg ) { element = arg; }
char uppercase ();
};
>
template <>
Ditch the line above. You are just defining a member that has already been
declared.
char container<char> ::uppercase()
{
if ((element>='a') &&(element<='z' ))
element+='A'-'a';
return element;
}
>
int main () {
container<intmy int (7);
container<charm ychar ('j');
cout << myint.increase( ) << endl;
cout << mychar.uppercas e() << endl;
return 0;
}
>
>
and these are the error messages (sorry, some of them are in spanish...)
>
templates3.c:17 : error: expected `)' before 'arg'
templates3.c:22 : error: el id de plantilla 'uppercase<>' para
'char container<char> ::uppercase()' no coincide con ninguna
declaración de plantilla
templates3.c:22 : error: declaración inválida de función
templates3.c: In function 'int main()':
templates3.c:31 : error: no se encuentra una función coincidente para
la llamada a 'container<char >::container(ch ar)'
templates3.c:14 : nota: los candidatos son: container<char> ::container()
templates3.c:14 : nota:
container<char> ::container(con st container<char> &)
>
>
Can anyone tell me what is going on. Thanks in advance.

Best

Kai-Uwe Bux
Sep 12 '06 #5
Josefo wrote:
Hello again (I am sorry...), but after spending a lot of time looking
to this easier example, I cannot find the error:
// class templates
#include <iostream>
using namespace std;
template <class T>
class pair {
T a, b;
public:
pair (T first, T second)
{a=first; b=second;}
T getmax ();
};
template <class T>
T pair <T>::getmax ()
{
T retval;
retval = a>b? a : b;
return retval;
}

int main () {
pair <intmyobject (100, 75);
cout << myobject.getmax ();
return 0;
}
When compiling, I get:

josemanuel@jose fo:~/C++$ c++ templates2.c
templates2.c:13 : error: expected initializer before '<' token
I simply does not understand where is he error. Everything looks OK in
this line. After trying several "fenomenologina l" modifications I give
up. Some hint?
Hint #1: don't top-post. Put your reply below or inline with the
message you're responding to.

Hint #2: there's a standard class template called std::pair, and your
"using namespace std;" has brought it into scope. Thus, you are
introducing ambiguity between your pair class and the standard one.
Either remove the using statement and explicitly qualify all members of
namespace std (cout is the only one you used here), qualify your pair
class with :: (this results in ugly code in your case since you also
need to wrap ::pair<T>::getm ax in parentheses to help the compiler
out), or rename your class (probably the best option).

Cheers! --M

Sep 12 '06 #6
Josefo wrote:
Hello all. I am a newbie following the C++ tutorial in :
[snip]
templates3.c:17 : error: expected `)' before 'arg'
<OT>
As a side note, you should avoid giving your C++ files
a ".c" extension or some compilers will assume you are
compiling C code, not C++.
</OT>

HTH,
- J.
Sep 15 '06 #7

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

Similar topics

15
425
by: Rob Ratcliff | last post by:
I'm compiling the latest version of a CORBA ORB called MICO on a Cray X1. It makes heavy use of templates and namespaces. Up until the link step, the C++ source code compiled flawlessly. But, when it tried to link, I got the attached warnings and then an error. Any ideas why the linker wouldn't see the objects in the library? They look like...
5
1880
by: jose luis fernandez diaz | last post by:
Hi, When I compiling the program below: #include <map> using namespace std; template<typename td1, typename td2, typename td3,typename td4> class Tarificador
5
1567
by: Py PY | last post by:
(Apologies if this appears twice. I posted it yesterday and it was held due to a 'suspicious header') I'm having a hard time trying to get a couple of tests to pass when compling Python 2.3.5 on Ubuntu Server Edition 6.06 LTS. I'm sure it's not too far removed from the desktop edition but, clearly, I need to tweak something or install some...
15
2933
by: John Nagle | last post by:
I've been installing Python and its supporting packages on a dedicated server with Fedora Core 6 for about a day now. This is a standard dedicated rackmount server in a colocation facility, controlled via Plesk control panel, and turned over to me with Fedora Core 6 in an empty state. This is the standard way you get a server in a colo...
60
4032
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On May 3, 8:09 am, apati...@gmail.com wrote: A programmer that uses Vista? :O Vista is a hog of an operating system. Downgrade to Windows XP or get yourself a Linux distro.
6
2248
by: msb_6 | last post by:
Currently I have a PHP extension thats all written and compiles under windows, but the PC I'm going to end up putting it on is running Ubuntu 8.04 (g++ 4.2.3). I've delved into PHP documentation and ended up with a simple config.m4 file and the process to compile. However when I try to compile I get loads of errors that I didn't get when...
0
7584
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
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
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
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
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
3643
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
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
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
0
925
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.