C++ 运行时错误问题

C++ 运行时错误问题,c++,arrays,templates,runtime-error,C++,Arrays,Templates,Runtime Error,这里有一些代码给了我一个运行时错误,我似乎无法修复。函数Length()计算点阵列中所有点之间的累积距离。它使用了一个以前定义的函数Distance(),我知道这个函数非常有效。有什么建议吗 以下是我的函数源代码: template<typename Point> //Length function double PointArray<Point>::Length() const { double total_length = 0;

这里有一些代码给了我一个运行时错误,我似乎无法修复。函数Length()计算点阵列中所有点之间的累积距离。它使用了一个以前定义的函数Distance(),我知道这个函数非常有效。有什么建议吗

以下是我的函数源代码:

template<typename Point>                //Length function
double PointArray<Point>::Length() const
{
    double total_length = 0;
    for (int i=0; i<Size(); i++)
    {
        total_length += (GetElement(i)).Distance(GetElement(i+1));
    }
    return total_length;
}
模板//长度函数
双点数组::Length()常量
{
双倍总长度=0;

对于(int i=0;i,您正在读取超出数组末尾的元素

for (int i=0; i<Size(); i++)
{
    total_length += (GetElement(i)).Distance(GetElement(i+1));
                                                      //^^^
}

您正在读取超出数组末尾的元素

for (int i=0; i<Size(); i++)
{
    total_length += (GetElement(i)).Distance(GetElement(i+1));
                                                      //^^^
}
试试这个:

template<typename Point>                //Length function
double PointArray<Point>::Length() const
{
    double total_length = 0;
    for (int i=0; i<Size()-1; i++)
    {               //^^^ otherwise, i+1 will be out of range
        total_length += (GetElement(i)).Distance(GetElement(i+1));
    }
    return total_length;
}
模板//长度函数
双点数组::Length()常量
{
双倍总长度=0;
对于(int i=0;i请尝试以下方法:

template<typename Point>                //Length function
double PointArray<Point>::Length() const
{
    double total_length = 0;
    for (int i=0; i<Size()-1; i++)
    {               //^^^ otherwise, i+1 will be out of range
        total_length += (GetElement(i)).Distance(GetElement(i+1));
    }
    return total_length;
}
模板//长度函数
双点数组::Length()常量
{
双倍总长度=0;

因为(inti=0;我知道。我从来没有想到过。非常感谢!哇。从来没有想到过。非常感谢!