简体   繁体   English

C++ 中的读访问冲突

[英]Read-Access-Violation in C++

That is my error.那是我的错误。 I try to keep it a little bit more organized in what code I provide and what I don't.我试图在我提供的代码和我不提供的代码中让它更有条理。 In general, I am writing a C++ OpenGL application that can test, if two figures layed out with matchsticks match.一般来说,我正在编写一个 C++ OpenGL 应用程序,它可以测试两个用火柴棒排列的数字是否匹配。 I do the interaction by a Test class, which only is the way I can create graphical interfaces on top of my OpenGL interface.我通过 Test 类进行交互,这只是我可以在我的 OpenGL 界面之上创建图形界面的方式。 Important here are the following lines of code:以下代码行很重要:

Figure fig1 = Figure();
Figure fig2 = Figure();
Figure* selected;

I have two figure objects, and one pointer.我有两个图形对象和一个指针。 I can select either 1 or 2 in an ImGui overlay, which will in turn set the selected pointer to fig1 or fig2 .我可以在 ImGui 叠加层中选择 1 或 2,这将依次将选定的指针设置为fig1fig2 I then do tests for mouse events, and if the mouse is clicked I can calculate the x and y position of the point I clicked.然后我对鼠标事件进行测试,如果单击鼠标,我可以计算我单击的点的xy位置。 That works perfectly.这完美地工作。

I then want to add a new line object in my figure.然后我想在我的图中添加一个新的线对象。 My figure is saved as a set of lines in a std::vector<line> .我的图被保存为std::vector<line>中的一组std::vector<line> For reference, I will add my whole figure header in the end.作为参考,我将在最后添加我的整个图形标题。 For now, I call these two functions when the mouse is pressed:现在,我在按下鼠标时调用这两个函数:

selected->SetWorking(posX, posY, m_Select);     //         
                                                // -> HIER STECKT NH 
if (m_Select)                                   //        FEHLER
    selected->AddLine();

As I understand, I now call the two functions on the object pointed to by the selected pointer.据我了解,我现在在所选指针指向的对象上调用这两个函数。 I will just show what happens after calling SetWorking() , which is where the exception originally is thrown.我将只展示在调用SetWorking()之后会发生什么,这是最初抛出异常的地方。 If I exclude both of the method calls, the program runs perfectly.如果我排除了这两个方法调用,程序就会完美运行。 If I have either one of them or both executed, this error is thrown.如果我执行了其中一个或两个,则会引发此错误。

void Figure::SetWorking(int x, int y, bool segment)
{
    if (segment)
    {
        p1 = Point(x, y);
    }
    p2 = Point(x, y);
}

This is my implementation of the function.这是我对函数的实现。 The segment is not really important for the problem.该段对于问题并不重要。 Segment selects whether the first point of a line, or the second is to be edited. Segment 选择是编辑直线的第一个点还是第二个点。 It just alternates: if I click once, the first point is edited.它只是交替:如果我单击一次,则编辑第一个点。 Then, after clicking the second time, the second is edited.然后,在第二次点击后,第二次被编辑。 With the right point selected, I try to build a pair of the calculated x and y coordinates, and when building the pair, the exception is thrown:选择正确的点后,我尝试构建一对计算出的xy坐标,在构建对时,抛出异常:

write access violation.
this was 0x18.

在此处输入图片说明

My Figure header:我的图标题:

#pragma once
#include <Windows.h>
#include <vector>
#include <Defines.h>



class Point
{
public:
    std::pair<int, int> coord;
    Point()
    {
        coord = std::pair<int, int>{ 0, 0 };
    }
    Point(int x, int y)
    {
        coord = std::pair<int, int>{ x, y };
    };
};

class Line
{
public:
    std::pair<Point, Point> pts;
    Line()
    {
        pts = std::pair<Point, Point>{ Point(), Point() };
    }
    Line(Point p1, Point p2)
    {
        pts = std::pair<Point, Point>{ (Point)p1, (Point)p2 };
    };
};

class Figure
{

private:
    std::vector<Line> lines;
    Point p1 = Point();
    Point p2 = Point();
    char* renderPath = FILEPATH;
public:
    Figure() {};
public:
    void DrawComponents();
    void Clear();
    void RemoveLine(int index);
    void AddLine();
    void SetWorking(int x, int y, bool segment); //true seg1, false seg2
    
    //void Render();
    
};

我只是忘了在构造函数中设置指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM