template-id does not match any template declaration

B

blueblueblue2005

Hi, below is the code I copy from c++ tutorial, it is about template
specialization, but when I compile, I got error message:

error: template-id `module<>' for `int mypair<int>::module()' does
not match any template declaration
tmp.cpp:28: error: syntax error before `{' token

I guess it might be that my compiler does not recognize this type of
using template??? I am using g++-3.3 compiler under Ubuntu linux. any
help will be appreciated

here is the code :

1 //Template specialization
2 #include <iostream>
3
4 using namespace std;
5
6 template <class T>
7 class mypair {
8 T value1, value2;
9 public:
10 mypair (T first, T second)
11 {value1=first; value2=second;}
12 T module () {return 0;}
13 };
14
15 template <>
16 class mypair <int> {
17 int value1, value2;
18 public:
19 mypair (int first, int second)
20 {
21 value1=first;
22 value2=second;
23 }
24 int module ();
25 };
26
27 template <>
28 int mypair<int>::module() {
29 return value1%value2;
30 }
31
32 int main () {
33 mypair <int> myints (100,75);
34 mypair <float> myfloats (100.0,75.0);
35 cout << myints.module() << '\n';

36 cout << myfloats.module() << '\n';
37 return 0;
38 }
 
J

Jonathan Mcdougall

blueblueblue2005 said:
Hi, below is the code I copy from c++ tutorial, it is about template
specialization, but when I compile, I got error message:

Don't put numbers on each line, it prevents us from compiling your
code. Just put a comment the offending line.
error: template-id `module<>' for `int mypair<int>::module()' does
not match any template declaration
tmp.cpp:28: error: syntax error before `{' token

I guess it might be that my compiler does not recognize this type of
using template??? I am using g++-3.3 compiler under Ubuntu linux. any
help will be appreciated

here is the code :

1 //Template specialization
2 #include <iostream>
3
4 using namespace std;
5
6 template <class T>
7 class mypair {
8 T value1, value2;
9 public:
10 mypair (T first, T second)
11 {value1=first; value2=second;}
12 T module () {return 0;}
13 };
14
15 template <>
16 class mypair <int> {
17 int value1, value2;
18 public:
19 mypair (int first, int second)
20 {
21 value1=first;
22 value2=second;
23 }
24 int module ();
25 };
26
27 template <>
28 int mypair<int>::module() {
29 return value1%value2;
30 }

Drop the "template <>". That's the syntax for an explicit
instantiation, not for a member function definition.

Remember that what goes before the :: is the class name. If the class
name is

template <class T>
class C;

then you'll have

template <class T>
void C<T>::f()
{
}

because the class name is "template <class T> C<T>".

If the class name is

template <>
class C<int>

then it is not a template class anymore. The name of that class is
C<int>:

void C<int>::f()
{
}
31
32 int main () {
33 mypair <int> myints (100,75);
34 mypair <float> myfloats (100.0,75.0);
35 cout << myints.module() << '\n';

36 cout << myfloats.module() << '\n';
37 return 0;
38 }


Jonathan
 
B

blueblueblue2005

hi, I solved the problem by comment out the definition at line 27
through line 30, instead, I moved the function definition inside the
class definition, and there is no compiler error, but why the compiler
does allow me to define the function outside the class definition???

here is the modefied code:

1 //Template specialization
2 #include <iostream>
3
4 using namespace std;
5
6 template <class T>
7 class mypair {
8 T value1, value2;
9 public:
10 mypair (T first, T second)
11 {value1=first; value2=second;}
12 T module () {return 0;}
13 };
14
15 template <>
16 class mypair <int> {
17 int value1, value2;
18 public:
19 mypair (int first, int second)
20 {
21 value1=first;
22 value2=second;
23 }
24 int module(){ return value1%value2; }
25 };
26
27 /*
28 template <>
29 int mypair<int>::module() {
30 return value1%value2;
31 }
32 */
33
34 int main () {
35 mypair <int> myints (100,75);
36 mypair <float> myfloats (100.0,75.0);
37 cout << myints.module() << '\n';
38 cout << myfloats.module() << '\n';
39 return 0;
40 }
 
V

Victor Bazarov

Jonathan said:
[..]
Drop the "template <>". That's the syntax for an explicit
instantiation, not for a member function definition.

No, it's the syntax for a full specialization. An explicit
instantiation does not have the angle brackets.

V
 
V

Victor Bazarov

blueblueblue2005 said:
hi, I solved the problem by comment out the definition at line 27
through line 30, instead, I moved the function definition inside the
class definition, and there is no compiler error, but why the compiler
does allow me to define the function outside the class definition???

Are you just starting with C++, moving over from Java? That's the C++
way -- declare in one place, define in another. You don't have to put
all your member function implementations into the class definition.

V
 
B

blueblueblue2005

sorry, I put my msg before I got Jonathan's post. thanks for all the
help. yeah, I did java programming before, now started c++, and always
interfered by java. thanks a lot again.
 

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,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top