creating template function

Discussion in 'C++' started by kaustubh, Aug 13, 2007.

  1. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    I created a template function called add
    like this
    template(class T)
    T add(T a, T b)
    {

    return a + b;

    }

    now this will work for number types like int float, and string , but will not work for char .
    how can i say if ( T is char ) or
    (a is char) (b is char )
    then
    concatnate(a,b)
    :)
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Are you saying it will not work or asking if it will work or not?
     
  3. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    It is not working for char, so i want to modify this template function when it is a type of char
    then it will join(a,b) . Like add('h','i') should give hi .
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Google for "partial specialization".
     
  5. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    dawei,
    thank you very very much. Can you please tell me what is the c or c++ function to join to char . For example in above example i want to join a and b char.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    strcpy.
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    And strcat.
     
  8. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    hello dawei,
    there is a problem the defination of strcat is
    Code:
    strcat(int * a, int *b)
    it will only take inside pointers ,so if you want it to work with add
    it will be

    Code:
     
    #include<iostream>
    #include<cstring>
    
    char *add(int *a, int *b)
    {
    strcat(a,b);
    return *a;
    }
    
    int main()
    {
    char a[] = "hi";
    char b[] = "world";
    cout<<endl<<add(a,b);
    return 0;
    }
    
    OUTPUT:
    hiworld 
    
    but with error and terminated 
    
    
     
  9. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Son, you're in a world of hurt. You really need to get some decent documentation. The declaration of strcat is as follows:

    char * strcat ( char * destination, const char * source );

    You are also writing C code in your C++. Why the hell would you do that? Although the C string functions will work, they have been deprecated. Your compiler is free to quit supporting them at any time.

    C++ has a string class. It does all that you need. It even overloads the '+' operator and uses it for concatenation. Further, it grows as needed, so you don't have to worry about overflow.

    If you are just messing around in an attempt to learn, that's fine. Write your own string class. Overload the "=", "==", "+", and "[]" operators. You'll learn a lot.

    Here is some functioning code that illustrates both approaches.
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    
    using std::cout;
    using std::endl;
    using std::string;
    
    int main()
    {
        // The following is actually C code.  
        // Why write C code in C++ ????
    
        char first [] = "Hello";  // In most modern systems, these will be write protected
        char second [] = "Bubba"; // In other words, you can't use them as a destination
        char destination [16];    // This must be large enough to hold the result
    
        strcpy (destination, first);
        strcat (destination, ", ");
        strcat (destination, second);
    
        // This is C++ code
        cout << first << "\n" << second << "\n" << destination << endl;
    
        string newDestination = first;
        newDestination += ", ";
        newDestination += second;
        cout << "\n" << newDestination << endl;
        return 0;
    }
    
    Here is the output:
     
  10. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    I understand now that it's better to use strings.
    I will try to convert it string and then concatenate.
    The problem i had is can we create something like char[] add(char a[],char b[]) or can
    we convert string to char array . The reason i am asking is because i have old projects containing char arrays , i sometimes need to concatenate them.
    Thank you very much for your help
    .
     
  11. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Well, as you can see, my program concatenated char arrays to both a char array and a string. You can get a char array from the string by using the .c_str () method.
     
  12. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    I am sorry i tried doing that but here is the code

    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
    const char add(char a[], char b[])
     {
    string str = a;
    str+= b;
    const char *c_str1 = str.c_str ( ); 
    return *c_str1;
    }
     int main()
    {
    char a[] = "hi";
    char b[] = "world";
    cout<<endl<<add(a,b);
    return 0;
    }
    
    OUTPUT:
    h
    
    you see output gives h when i want "hi world ",please help how should i change the function.
     
  13. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You have defined your add function to return a single char. You are then returning the dereferenced value of the string. The first byte of that string is a single char, precisely as you have asked it to do.

    At some point you are going to have to realize that your code does as you instruct it to do, not as you wish that it would do in the absence of explicit instructions.
     
  14. kaustubh

    kaustubh New Member

    Joined:
    Aug 13, 2007
    Messages:
    25
    Likes Received:
    0
    Trophy Points:
    0
    Sorry but how should i define my function where should i change.
    Thankyou for your reply.
     
  15. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    You cannot declare a local variable in a function and return a pointer to it. The variable goes out of scope when the function returns. The pointer then points to junk. You either need to declare the variable outside the function (as I did with "destination"), or you need to dynamically allocate memory for it inside the function. That memory, not being local, will persist. In the latter case, you must remember to free the memory.

    I suggest you make a class for string manipulation and have the add method be part of that class. Then the destructor would be used to free the memory.

    You are missing some very basic understanding of the language. You need to study. It will be more time-effective then bopping in and out of a forum for every little thing you do wrong.
     
  16. jwshepherd

    jwshepherd New Member

    Joined:
    Aug 13, 2007
    Messages:
    135
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VP Technology Corporation
    Location:
    Texas
    Home Page:
    http://www.officialhackers.com
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    
     int main()
    {
    char a[] = "hi ";
    char b[] = "world";
    cout<<endl<<strcat(a,b);
    return 0;
    }
     
  17. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    JW, your solution corrupts variable a and relies on a specific method of auto-variable generation to prevent it from corrupting other memory locations and causing a crash.
     
  18. jwshepherd

    jwshepherd New Member

    Joined:
    Aug 13, 2007
    Messages:
    135
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    VP Technology Corporation
    Location:
    Texas
    Home Page:
    http://www.officialhackers.com
    sorry, it was quick and dirty modifying the code he had. Hopefully he gets the idea
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice