problems compiling templates with ubuntu c++

J

Josefo

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<int> myint (7);
container<char> mychar ('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.
 
J

Jerry Coffin

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).
 
K

Kai-Uwe Bux

Josefo said:
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<int> myint (7);
container<char> mychar ('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
 
J

Josefo

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

Josefo
 
J

Josefo

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 <int> myobject (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?
 
M

mlimber

Josefo said:
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 <int> myobject (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
 
J

Jacek Dziedzic

Josefo said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top