By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,587 Members | 1,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,587 IT Pros & Developers. It's quick & easy.

problems compiling templates with ubuntu c++

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.