Thread: template question

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15

    Angry template question

    hi :

    while compiling ,i confronted a problem in template.

    here is the code ,very short.

    The codes can be compiled through,however there are warnings .

    i was confused by these warnings.
    Code:
    #include<iostream>
    using namespace std;
    
    template <class T>
    T &max(T x[],int n)
    {int i;
      T maxv=x[0];
      for(i=1;i<n;i++)
        if(maxv<x[i])
          maxv=x[i];
      return maxv;
    }
    
    int main()
    {
      int a[]={4,5,2,8,9,3};
      double b[]={3.5,6.7,5.2,9.2};
      cout<<"max of a is:"<<max(a,6)<<endl;
      cout<<"max of b is:"<<max(b,4)<<endl;
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2008
    Posts
    14
    I guess that the warning is about returning a reference to a local variable. It has nothing to do with template. When you return a reference from a function, make sure the variable is still alive when leaving the function.

    Quote Originally Posted by wxjeacen View Post
    hi :

    while compiling ,i confronted a problem in template.

    here is the code ,very short.

    The codes can be compiled through,however there are warnings .

    i was confused by these warnings.
    Code:
    #include<iostream>
    using namespace std;
    
    template <class T>
    T &max(T x[],int n)
    {int i;
      T maxv=x[0];
      for(i=1;i<n;i++)
        if(maxv<x[i])
          maxv=x[i];
      return maxv;
    }
    
    int main()
    {
      int a[]={4,5,2,8,9,3};
      double b[]={3.5,6.7,5.2,9.2};
      cout<<"max of a is:"<<max(a,6)<<endl;
      cout<<"max of b is:"<<max(b,4)<<endl;
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15

    Angry

    Quote Originally Posted by plutino View Post
    I guess that the warning is about returning a reference to a local variable. It has nothing to do with template. When you return a reference from a function, make sure the variable is still alive when leaving the function.
    however, how can i change the codes to pass these warnings?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by wxjeacen View Post
    however, how can i change the codes to pass these warnings?
    retrn the value not the reference
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15

    Angry

    Quote Originally Posted by vart View Post
    retrn the value not the reference
    would you please type the codes.....

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You just need to type a backspace

    Actually, if you really want to return a reference, you can do so by returning an element in the array. This means that instead of keeping track of the value of the max element, you keep track of the index of the max element.

    Oh, and were you aware that std::max_element already does roughly what you are trying to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15
    Quote Originally Posted by laserlight View Post
    You just need to type a backspace

    Actually, if you really want to return a reference, you can do so by returning an element in the array. This means that instead of keeping track of the value of the max element, you keep track of the index of the max element.

    Oh, and were you aware that std::max_element already does roughly what you are trying to do?
    understand....

    give my deepest thank to all of you .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. another template question
    By l2u in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2008, 03:52 PM
  4. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 11:43 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM

Tags for this Thread