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

template-id does not match any template declaration

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

Jul 23 '05 #1
Share this Question
Share on Google+
6 Replies


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

Jul 23 '05 #2

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

Jul 23 '05 #3

P: n/a
Jonathan Mcdougall wrote:
[..]
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
Jul 23 '05 #4

P: n/a
blueblueblue2005 wrote:
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
Jul 23 '05 #5

P: n/a
> but why [doesn't] the compiler
does allow me to define the function outside the class definition???
because you're not listening to Jonathan?
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()
{

}


Jul 23 '05 #6

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

Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.