template question

This is a discussion on template question within the C++ Programming forums, part of the General Programming Boards category; hi : while compiling ,i confronted a problem in template. here is the code ,very short. The codes can be ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    Quote Originally Posted by wxjeacen View Post
    however, how can i change the codes to pass these warnings?
    retrn the value not the reference
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    21,397
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02:52 PM
  4. Quick question about class template
    By merixa in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2005, 10:43 PM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

Tags for this Thread


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