以下代码在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;
}

但是,在Linux中编译时,会出现以下错误:
In function 'void find_points(), error: expect ';' before 'it_max'
'it_max' was not declared in this scope

有什么想法吗?谢谢。

最佳答案

typename之前需要std::vector<Coordinate<T> >::const_iterator,因为std::vector<Coordinate<T> >是一个依赖作用域(即:您需要告诉编译器std::vector<Coordinate<T> >::const_iterator将是一个类型)
所以:

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


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

(根据我的编译器,这是编译的)
是的,请看@gassa的评论了解原因:)

关于c++ - 预期为“;”在LINUX(c++)中的迭代器之前,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22858294/

10-12 02:58