0

The following codes can run very well in windows:

template <typename T>
    class  Coordinate  
    {
    public:
        T x_;  ///< x_coordinate
        T y_;  ///< y_coordinate
    };


        template<typename T>
        struct compare_x_coordinate 
        {
            bool operator() (const Coordinate<T> &i,const Coordinate<T> &j) 
            { return i.x_<j.x_; }
        } ;

        template<typename T>
        struct compare_y_coordinate 
        {
            bool operator() (const Coordinate<T> &i,const Coordinate<T> &j) 
            { return i.y_<j.y_; }
        } ;




    template<typename T >
    void find_points(const std::vector<Coordinate<T> > &ptArray, 
        Coordinate<T> &left, 
        Coordinate<T> &right
        )
    {
        compare_x_coordinate<T> mycompare; 
        std::vector<Coordinate<T> >::const_iterator it_max = std::max_element(ptArray.begin(), ptArray.end(), mycompare);
        int index_max = it_max-ptArray.begin();


        std::vector<Coordinate<T> >::const_iterator it_min = std::min_element(ptArray.begin(),ptArray.end(),mycompare); 
        int index_min = it_min-ptArray.begin();

        left    = ptArray[index_min];
        right   = ptArray[index_max];
    } ;


int main(void)
{
     std::vector<Coordinate<float> > ptArray;
     Coordinate<float> pt;
     pt.x_ = 20;
     pt.y_ = 15;
     ptArray.push_back(pt);

     pt.x_ = 3;
     pt.y_ = 200;
      ptArray.push_back(pt);

     pt.x_ = 7;
     pt.y_ = 2;
      ptArray.push_back(pt);

     pt.x_ = 12;
     pt.y_ = 500;
      ptArray.push_back(pt);

      Coordinate<float> left;
      Coordinate<float> right;
      find_points<float>(ptArray,left,right);

    return 0;
}

However, when it is compiled in Linux, the following errors occur:

In function 'void find_points(), error: expect ';' before 'it_max'      
'it_max' was not declared in this scope

Any ideas? Thanks.

Kaz Dragon
  • 6,681
  • 2
  • 36
  • 45
feelfree
  • 11,175
  • 20
  • 96
  • 167
  • 1
    Here's a duplicate which also tries to explain _why_, not only _how_: http://stackoverflow.com/questions/6571381/dependent-scope-and-nested-templates. – Gassa Apr 04 '14 at 09:15
  • Do you mean Visual Studio vs gcc (different compilers and toolchains) by Windows vs Linux? – sashoalm Apr 04 '14 at 11:32

2 Answers2

3

Youn need typename before std::vector<Coordinate<T> >::const_iterator because std::vector<Coordinate<T> > is a dependent scope (ie: you need to tell the compiler that std::vector<Coordinate<T> >::const_iterator will be a type)

so:

    typename std::vector<Coordinate<T> >::const_iterator it_max = 
              std::max_element(ptArray.begin(), ptArray.end(), mycompare);

and

    typename std::vector<Coordinate<T> >::const_iterator it_min = 
              std::min_element(ptArray.begin(),ptArray.end(),mycompare); 

(according to my compiler this compiles)

And yes, see @Gassa's comment for the why part :)

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
1

In a template, some names that are depended on template parameter are called dependent names. when dependent names are nested in a class, wre call them nested dependent name. nested dependent name is difficult for complier to parse.

std::vector<Coordinate<T> >::const_iterator It maybe parse like a variable named const_iterator in a class std::vector<Coordinate<T> .so we must use typename before to declare std::vector<Coordinate<T>::const_iterator is a type.

wangsquirrel
  • 135
  • 2
  • 12