template id does not match any template declaration?

A

Alex Buell

This is what I'm trying to compile with GCC 3.4.4 under Linux. The
error I'm getting is:

g++ -s -O3 -Wall class2.cc -o class2
class2.cc:25: error: template-id `uppercase<>' for `char
container<char>::uppercase()' does not match any template declaration
class2.cc:25: error: invalid function declaration make: *** [class2]
Error 1

What's wrong with it? The code is as below. Thanks for any hints
offered.

#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 (char 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;
}
 
V

Victor Bazarov

Alex said:
This is what I'm trying to compile with GCC 3.4.4 under Linux. The
error I'm getting is:

g++ -s -O3 -Wall class2.cc -o class2
class2.cc:25: error: template-id `uppercase<>' for `char
container<char>::uppercase()' does not match any template declaration
class2.cc:25: error: invalid function declaration make: *** [class2]
Error 1

What's wrong with it? The code is as below. Thanks for any hints
offered.

#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 (char arg) { element = arg; }
char uppercase();
};

template<>

Drop the line above. What you're doing here is _definining_, not
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;
}

V
 
A

Alex Buell

Drop the line above. What you're doing here is _definining_, not
_specialising_. You've already specialised it. Now, 'template<>' is
extraneous.

Thank you, that did the trick. I haven't coded C++ for a very long time
and I'm trying to get back into it, with all those new and wonderful
features that's been added since the early 1990s.

Cheers,
Alex
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top