Templates:

This is a discussion on Templates: within the C++ Programming forums, part of the General Programming Boards category; Line 45-47 is not asking quite right. I am returning the address but now to sure why. I defined it ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468

    Angry Templates:

    Line 45-47 is not asking quite right. I am returning the address but now to sure why. I defined it as a char and I might be very new to Templates but I thought this is how they are suppose to work.....

    Code:
     #include <iostream>
    
    using namespace std;
    
    template <class T>
    class display {
        
    private:
        T first;
        T second;
        
    public:
        display(T a, T b)
        :first(a), second(b)
        {  }
        
        T largest();
        T characters();
        T sentence();
    };
    
    template<class T>
    
    T display<T>::largest()
    {
        return first>second?first:second;
    }
    template<class T>
    T display<T>::characters()
    {
        return first + second;
    }
    template<class T>
    T display<T>::sentence()
    {
        return first + second;
    }
    
    int main()
    {
        cout<<"The largest value will be displayed\n";
        display<int>HW(22,45);
        cout<<HW.largest()<<endl;
        
        cout<<"\nUsing a Template we will combine two Char's\n";
        display<char>HW1('H','W');
        cout<<HW1.characters()<<endl;
        
        cout<<"\nUsing a Template we will combine two String's\n";
        display<string>HW2("Home"," Work");
        cout<<HW2.sentence()<<endl;
        
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your characters() function is defined to return T, which for this case is a char, so you will only ever get a single char. On characters, plus just adds ASCII values.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,425
    You (or your code) is not returning any address at line 47.

    The output of line 47 would be a character who's numeric value is the sum of the characters 'H' and 'W'. Assuming your compiler supports the ASCII character set, 'H' has a value of 72, 'W' has a value of 87, so their sum is 159. That actually overflows a signed char type (so is formally undefined behaviour), but will probably be one of the extended ASCII characters on output. If your compiler supports some other character set, the character output will be different. That will explain the funny output you are seeing.

    Your problem has little actually to do with templates. It is that you don't understand the difference between a character (which is an integral type) and a string (which represents a collection of characters).
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    I understand what you have said and adjusted the code. However I have to enter two values so it meets the constructor. This look ugly places two char in line 46 when I only want it to return one char. Is there a better way to do this?

    Code:
    #include <iostream>
    
    using namespace std;
    
    template <class T>
    class display {
        
    private:
        T first;
        T second;
        
    public:
        display(T a, T b)
        :first(a), second(b)
        {  }
        
        T largest();
        T characters();
        T sentence();
    };
    
    template<class T>
    
    T display<T>::largest()
    {
        return first>second?first:second;
    }
    template<class T>
    T display<T>::characters()
    {
        return first;
    }
    template<class T>
    T display<T>::sentence()
    {
        return first + second;
    }
    
    int main()
    {
        cout<<"The largest value will be displayed\n";
        display<int>HW(22,45);
        cout<<HW.largest()<<endl;
        
        cout<<"\nUsing a Template we will return a Char\n";
        display<char>HW1('H','W');
        cout<<HW1.characters()<<endl;
        
        cout<<"\nUsing a Template we will combine two String's\n";
        display<string>HW2("Home"," Work");
        cout<<HW2.sentence()<<endl;
        
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    468
    Code:
      cout<<"\nThe Template we will return a Char:\n";
        display<char>HW1('H','\0');
        cout<<HW1.characters()<<endl;
    Is this better programming practice?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This is homework (or else there a lot more "HW"s in the code than I would expect at random), so you should follow those specs (whatever they are).

    Having said that, the name "characters" suggest you should be returning, well, characters, which means, if it were me making this assignment, I would have specified something like
    Code:
    template<class T>
    std::string display<T>::characters()
    {
        //this function creates a string that is the concatenation of the two data fields first and second
        //each data field should be displayed in its "normal" manner, ie the way << would print it out
        return "something";
    }
    That is to say, it doesn't seem to make a lot of sense to have a function called "characters" return an int, or a char, or Random_Type_T.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,425
    Quote Originally Posted by jocdrew21 View Post
    Code:
      cout<<"\nThe Template we will return a Char:\n";
        display<char>HW1('H','\0');
        cout<<HW1.characters()<<endl;
    Is this better programming practice?
    No, because you are just randomly guessing. Good programming practice is systematic.

    tabstop and I have described why your code is behaving as it is. You somehow seem to expect that adding the characters 'H' and 'W' will produce the string "HW", even though your template function places the results into a single character. That is not a template problem per-se - it is a problem with understanding the difference between characters and strings, and not guessing.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. templates and g++
    By coletek in forum C++ Programming
    Replies: 8
    Last Post: 01-12-2009, 03:10 AM
  2. Help with templates
    By hanniball in forum C++ Programming
    Replies: 1
    Last Post: 12-26-2007, 05:16 PM
  3. Templates
    By creativeinspira in forum C++ Programming
    Replies: 7
    Last Post: 08-07-2007, 11:04 PM
  4. help with templates
    By drdodirty2002 in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2004, 05:25 AM
  5. Templates - why?
    By nickname_changed in forum C++ Programming
    Replies: 5
    Last Post: 03-23-2003, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21