Thread: newbie class templates question

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    3

    newbie class templates question

    Hi there,

    This is my 1st post on this board, so plz donīt hesitate
    to welcome me by replying massively to the question
    below concerning a class template implementation.
    I included only part of the code:

    template <class T>
    class pr {
    T value1, value2;
    public:
    pr (T first, T second)
    {value1=first; value2=second;}
    T module () {return 0;}
    };

    template <>
    class pr<int>{
    int value1, value2;
    public:
    pr (int first, int second)
    {value1=first; value2=second;}
    int module ();
    };

    template <>
    int pr<int>::module() {
    return value1%value2;
    }




    the BOLD part of the code returns the following error
    while compiling in borlands C5.5 compiler:

    invalid explicit specialization of 'pr<int>::module()' etc.

    Did I overlook something

    Thanx,

    Martijn

  2. #2
    well i dunno
    -Save the whales. Collect the whole set.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    template <>
    class pr<int>{
    int value1, value2;
    public:
    pr (int first, int second)
    {value1=first; value2=second;}
    int module ();
    };

    template <>
    int pr<int>::module() {
    return value1%value2;
    }

    invalid explicit specialization of 'pr<int>::module()' etc

    you don't need a template here if it is always going to be an int returned. I don't think you want the modulus of two chars, etc. You may want to make a template if your going to use numeric types other than ints such as long, if that is the case just use the regular template syntax. And since % only works on integral type int, long, char, unsigned char... a template won't work very well. Such as a double won't work with the % operator.

    Code:
    class pr
    { 
         int value1, value2; 
    public: 
         pr (int first = 1, int second = 1) 
             {value1=first; value2=second;} 
        int module (); 
    }; 
    
    int pr::module() { 
    return value1%value2; 
    }
    if you want a template your original ( with initialization in constructor which causes problems with some data type obviously) :

    Code:
    template <class T> 
    class pr { 
    T value1, value2; 
    public: 
    pr (T first = 1, T second = 1) 
    {value1=first; value2=second;} 
    T module () {value1 % value2;} 
    }; 
    
         pr <int> nPr;
         pr<char> cPr; //will work not much value
         pr<string> sPr; //will not work the constructor cannot use the string template becuase of the constructor uses int
         pr<double> d1( 10, 4 );//will work
        d1.module( );//fails on compile % is not an operator for double
    Basically this is not a good use of a template, its not needed here.

    You'll also have to do more checking in your module method to avoid dividing by zero. Bad Ju Ju...

    dang

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie class question
    By Beowolf in forum C++ Programming
    Replies: 1
    Last Post: 09-22-2007, 09:03 PM
  2. C++ Newbie - Derived Class Issue
    By Gipionocheiyort in forum C++ Programming
    Replies: 8
    Last Post: 08-01-2007, 12:20 AM
  3. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM