c++ - vector , "virtual",函数调用时出现段错误

标签 c++

当我试图调用一个对象内的函数时,我遇到了一个段错误,该对象是“形状”指针 vector 的一部分

我的问题出在这个函数中::

    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;
    }

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

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;
}

所以我有一个全局的“形状” vector 。 vector 形状;

我有一个类形状

#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

还有一个 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;
    }
};

最佳答案

问题出在这里:

Sphere s(Point(0.0,0.0,-50.0), 40.0);
shapes.push_back(&s);

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

相反,做

Sphere *s = new Sphere(Point(0.0,0.0,-50.0), 40.0);
shapes.push_back(s);

从堆中分配球体,使其持久存在。确保在完成后将其删除

关于c++ - vector , "virtual",函数调用时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1858963/

相关文章:

c++ - 如何调试 openGL 代码?

c++ - C++代码说明

c++ - QML 访问不可调用函数

c++ - 虚函数调用非虚函数,反之亦然

c++ - 我应该专攻还是重载?

c# - COM 接口(interface)将 COM 回调传递给 .Net 应用程序时出现内存泄漏

c++ - DirectX 11 渲染到特定区域

c++ - 编辑文本文件

c++ - 如何为文本设置计时器?

c++ - 将 C++ unordered_map 序列化到缓冲区 (char*)