template-id does not match any template declaration

Discussion in 'C++' started by blueblueblue2005, Jul 19, 2005.

  1. 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 }
     
    blueblueblue2005, Jul 19, 2005
    #1
    1. Advertising

  2. 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
     
    Jonathan Mcdougall, Jul 19, 2005
    #2
    1. Advertising

  3. 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 }
     
    blueblueblue2005, Jul 19, 2005
    #3
  4. 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
     
    Victor Bazarov, Jul 19, 2005
    #4
  5. 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
     
    Victor Bazarov, Jul 19, 2005
    #5
  6. blueblueblue2005

    Alipha Guest

    > 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()
    > {
    >
    > }
     
    Alipha, Jul 19, 2005
    #6
  7. 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.
     
    blueblueblue2005, Jul 19, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    974
  2. kelvSYC
    Replies:
    6
    Views:
    7,281
    Richard Herring
    May 17, 2005
  3. Alex Buell
    Replies:
    2
    Views:
    674
    Alex Buell
    Feb 21, 2006
  4. Joseph Turian
    Replies:
    2
    Views:
    869
    Joseph Turian
    Mar 22, 2007
  5. Klaus
    Replies:
    3
    Views:
    1,110
    Klaus
    Mar 31, 2008
Loading...

Share This Page