Thread: Help-about how to use "template"

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28

    Help-about how to use "template"

    Hi friends,
    I got a complie error for the following code.
    Please help me,thanks a lot!
    Code:
    #include <iostream>
    using namespace std;
    template <class T,class T1> T addin(T a,T1 b)
    {
    	return(a+b);
    }
    float addin(float a,int b);//error at this line
    int main(void)
    {
    	int i=1;
    	float f1=10.5;
    	cout<<addin(f1,i)<<endl;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And if you delete this line?
    Code:
    float addin(float a,int b);//error at this line
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28
    Hi Dave,
    if deleting the line the result is PASS(vs05 and cb07)

  4. #4
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28
    I also found that:
    if using the following code
    Code:
    #include <iostream>
    using namespace std;
    template <class T,class T1> T addin(T a,T1 b)
    {
    	return(a+b);
    }
    float addin(float a,int b)
    {
    //	return(a+b);
    	cout<<"Using the reloaded function!\n";
    }
    int main(void)
    {
    	int i=1;
    	float f1=10.5,f2=11.2;
    	cout<<addin(f1,i)<<endl;
    	cout<<addin(f1,f2)<<endl;
    }
    The output is :
    Using the reloaded function!
    -1.#IND
    21.7

    If using :
    Code:
    #include <iostream>
    using namespace std;
    template <class T,class T1> T addin(T a,T1 b)
    {
    	return(a+b);
    }
    float addin(float a,int b)
    {
    	return(a+b);
    	cout<<"Using the reloaded function!\n";//this line can't be shown
    }
    int main(void)
    {
    	int i=1;
    	float f1=10.5,f2=11.2;
    	cout<<addin(f1,i)<<endl;
    	cout<<addin(f1,f2)<<endl;
    }
    The output is:
    11.5
    21.7

    I feel strange about this. Can anyone help to explain them? Tks.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    With the first one, you don't return anything from the function 'float addin(float a,int b)'.
    With the second one, you return before you print the text.
    Code:
    #include <iostream>
    using namespace std;
    template <class T,class T1> T addin(T a,T1 b)
    {
    	return(a+b);
    }
    float addin(float a,int b)
    {
    	cout<<"Using the reloaded function!\n";//this line can't be shown
    	return(a+b);
    }
    int main(void)
    {
    	int i=1;
    	float f1=10.5,f2=11.2;
    	cout<<addin(f1,i)<<endl;
    	cout<<addin(f1,f2)<<endl;
    }
    /* my output
    Using the reloaded function!
    11.5
    21.7
    */
    :shrug:
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28
    Tks, got it.
    But why would occur compile error if adding the line
    Code:
    float addin(float a,int b);

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It's a prototype -- but you supply no definition.
    Code:
    #include <iostream>
    using namespace std;
    template <class T,class T1> T addin(T a,T1 b)
    {
    	return(a+b);
    }
    int main(void)
    {
    	int i=1;
    	float f1=10.5;
    	cout<<addin(f1,i)<<endl;
    }
    /* my output
    11.5
    */
    Is what I see if I get rid of the prototype (or else also provide a function definition).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Feb 2008
    Location
    China
    Posts
    28
    Thanks a lot for ur help.
    I got similar codes on a book and it should be wrong.
    Last edited by chenayang; 03-17-2008 at 10:16 PM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also note that 10.5 is a double, not a float.
    10.5f is a float.
    It's always a good idea to use the correct type when assigning.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed