向量,"虚拟",函数调用上的分段错误

1 c++

我试图调用一个对象内的函数时出现分段错误,该对象是"形状"指针向量的一部分

我的问题在于这个功能::

    Point findIntersection(Point p, Point vecDir, int *status)
    {

        Point noPt;
        for (int i = 0; i < shapes.size(); i++)
        {
            Point temp;
            cout << "Shapes size" << shapes.size() << endl;
**SEGMENTATIONFAULT HERE >>>>>**            bool intersect = shapes[0]->checkIntersect(p, vecDir, &temp);
            if (intersect)
            {
                *status = 1;    // Code 1 for intersecting the actual shape
                return temp;
            }

        }

        return noPt;
    }
Run Code Online (Sandbox Code Playgroud)

最初,我只添加一个形状:

void createScene()
{

    image = QImage(width, height, 32); // 32 Bit

    Sphere s(Point(0.0,0.0,-50.0), 40.0);
    shapes.push_back(&s);
    cout << shapes.size() <<endl;
}
Run Code Online (Sandbox Code Playgroud)

所以我有一个全局的"形状"向量.矢量形状;

我有一个班级形状

#include "Point.h"
#ifndef SHAPE_H
#define SHAPE_H
using namespace std;
class Shape
{
    public: 
    Shape() {}
    ~Shape(){}
    virtual bool checkIntersect(Point p, Point d, Point *temp) {};  // If intersects, return true else false.
    virtual void printstuff() {};

};
#endif
Run Code Online (Sandbox Code Playgroud)

还有一个Sphere类

#include "shape.h"
#include <math.h>
#include <algorithm>
using std::cout;
using std:: endl;
using std::min;

class Sphere : public Shape
{
    public:
    Point centerPt;
    double radius;

    Sphere(Point center, double rad)
    {
        centerPt = center;
        radius = rad;
    }

    bool checkIntersect(Point p, Point vecDir, Point *temp)
    {
        cout << "Hi" << endl;
    /*
        Point _D = p - centerPt;
        double a = Point :: dot(vecDir, vecDir);
        double b = 2 * ( Point :: dot(vecDir, _D) );
        double c = (Point :: dot(_D,_D)) - (radius * radius);

        // Quadratic Equation
        double tempNum = b * b - 4 * a * c;
        if (tempNum < 0)
        {
            return false;
        } else
        {
            double t1 = ( -b + sqrt(tempNum) ) / (2 * a);
            double t2 = ( -b - sqrt(tempNum) ) / (2 * a);
            double t;

            if (t1 < 0 && t2 > 0) { t = t2; }
            else if (t2 < 0 && t1 > 0) { t = t1; }
            else if ( t1 < 0 && t2 < 0 ) { return false; }
            else
            {
                t = min(t1, t2);
            }

            Point p1 = p + (vecDir * t);

            if (p1.z > 0)           // Above our camera
            {
                return false;
            } else 
            {
                temp = &p1;
                return true;
            }

        }
    */
        return false;
    }
};
Run Code Online (Sandbox Code Playgroud)

moo*_*dow 7

问题出在这里:

Sphere s(Point(0.0,0.0,-50.0), 40.0);
shapes.push_back(&s);
Run Code Online (Sandbox Code Playgroud)

此时,您已在s堆栈上本地创建了Sphere ,并且已将其地址推送到向量中.当您离开作用域时,将释放本地对象,因此您存储在向量中的地址现在指向您不再拥有的内存,并且其内容未定义.

相反,做

Sphere *s = new Sphere(Point(0.0,0.0,-50.0), 40.0);
shapes.push_back(s);
Run Code Online (Sandbox Code Playgroud)

从堆中分配Sphere以使其保持不变.完成后一定要确保delete它.