473,472 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Create 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<intmyint (7);
container<charmychar ('j');
cout << myint.increase() << endl;
cout << mychar.uppercase() << 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(char)'
templates3.c:14: nota: los candidatos son: container<char>::container()
templates3.c:14: nota:
container<char>::container(const container<char>&)
Can anyone tell me what is going on. Thanks in advance.

Sep 10 '06 #1
6 2579
In article <11**********************@m79g2000cwm.googlegroups .com>,
jo**********@gmail.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<intmyint (7);
container<charmychar ('j');
cout << myint.increase() << endl;
cout << mychar.uppercase() << 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(char)'
templates3.c:14: nota: los candidatos son: container<char>::container()
templates3.c:14: nota:
container<char>::container(const 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<intmyint (7);
container<charmychar ('j');
cout << myint.increase() << endl;
cout << mychar.uppercase() << 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(char)'
templates3.c:14: nota: los candidatos son: container<char>::container()
templates3.c:14: nota:
container<char>::container(const 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@josefo:~/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 "fenomenologinal" 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<intmyint (7);
container<charmychar ('j');
cout << myint.increase() << endl;
cout << mychar.uppercase() << 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(char)'
templates3.c:14: nota: los candidatos son: container<char>::container()
templates3.c:14: nota:
container<char>::container(const 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@josefo:~/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 "fenomenologinal" 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>::getmax 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
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...
5
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
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...
15
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,...
60
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.