Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么我的指针向量不断导致EXC\u BAD\u访问?_C++_Xcode_Vector_Std - Fatal编程技术网

C++ 为什么我的指针向量不断导致EXC\u BAD\u访问?

C++ 为什么我的指针向量不断导致EXC\u BAD\u访问?,c++,xcode,vector,std,C++,Xcode,Vector,Std,我试图使用xcode创建有限自动机的图形表示,因此我创建了状态和转换类。为了使移动对象变得容易,我已经包含了一组进入和离开状态的转换指针。编译很简单,但是当我尝试附加到向量时,它会产生以下错误EXC错误访问(代码=1,地址=0x3f35) 下面的错误将带我到std库,并显示该行中的错误 template <class _Tp, class _Allocator> inline _LIBCPP_INLINE_VISIBILITY void vector<_Tp, _Allocat

我试图使用xcode创建有限自动机的图形表示,因此我创建了状态和转换类。为了使移动对象变得容易,我已经包含了一组进入和离开状态的转换指针。编译很简单,但是当我尝试附加到向量时,它会产生以下错误<代码>EXC错误访问(代码=1,地址=0x3f35) 下面的错误将带我到std库,并显示该行中的错误

template <class _Tp, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
void
vector<_Tp, _Allocator>::push_back(const_reference __x)
{
    if (this->__end_ != this->__end_cap())
    {
        __annotate_increase(1);
        __alloc_traits::construct(this->__alloc(),
                                  _VSTD::__to_raw_pointer(this->__end_), __x);
        ++this->__end_;
    }
    else
        __push_back_slow_path(__x);
}
模板
内联\u LIBCPP\u内联\u可见性
无效的
向量::推回(常量引用)
{
如果(this->\uuuuu end\uu!=this->\uuuuu end\u cap())
{
__注释_增加(1);
__alloc_traits::construct(this->alloc(),
_VSTD::uuu到_u原始_指针(此->uuu结束),uux);
++这个->结束;
}
其他的
__向后推慢路径(uux);
}
这里是我的状态类的简化版本,我的转换类在之前声明,然后在之后定义

class State
{
    int id;
    std::vector<Transition *> links_in;
    std::vector<Transition *> links_out;
    float x;
    float y;
    int r = radius; //x, y are centre coordinates of the circle representing the state, while r is the radius
    bool is_active = false;
    bool is_end = false;
    bool is_shown = true;
    bool is_moving;
public:
    // Get Functions go here

    // Set Functions go here

    //Add functions
    void add_in_trans(Transition * t){
        links_in.push_back(t);
    }
    void add_out_trans(Transition * t){
        links_out.push_back(t);
    }
    //Delete Functions
    void remove_in_trans(){
        links_in.pop_back();
    }
    void remove_out_trans(){
        links_out.pop_back();
    }
    void draw_state();
    State(int ix, int iy);
    State(){}
}
类状态
{
int-id;
std::矢量链接;
std::向量链接输出;
浮动x;
浮动y;
int r=radius;//x,y是表示状态的圆的中心坐标,而r是半径
bool为激活状态=错误;
bool是_end=false;
布尔值显示为真;
布尔在移动;
公众:
//让我们到这里来
//设置函数到这里
//添加函数
无效添加到转换中(转换*t){
链接在。推回(t);
}
无效添加输出转换(转换*t){
向外链接。向后推(t);
}
//删除函数
在_trans()中删除_{
links_in.pop_back();
}
void remove_out_trans(){
links_out.pop_back();
}
void draw_state();
州(int ix,int iy);
状态(){}
}
如果你对更好的方法有什么建议,我很高兴听到。我花了一整天的时间试图解决这个问题,但毫无结果

提前谢谢

更新:

我尝试使用整数和向量作为临时修复,但我遇到了相同的问题,所以我假设问题不是指针,而是我使用向量的方式

这是密码

#include <vector>

class Transition;

class State
{
    int id;
    std::vector<int> links_in;
    std::vector<int> links_out;
    float x;
    float y;
    int r = radius; //x, y are centre coordinates of the circle representing the state, while r is the radius
    bool is_active = false;
    bool is_end = false;
    bool is_shown = true;
    bool is_moving;
public:
    // Get Functions
    int get_x(){
        return x;
    }
    int get_y(){
        return y;
    }
    int get_id(){
        return id;
    }
    bool is_it_active(){
        return is_active;
    }
    bool is_it_moving(){
        return is_moving;
    }
    bool is_in(int ix, int iy){ //Function to tell if pair of coordinates are in the circle, used to select.
        std::cerr << ix-x << " " << iy-y << " " << r*r << std::endl;
        if ((ix-x)*(ix-x) + (iy-y)*(iy-y) < r*r)
            return true;
        else
            return false;
    }
    // Set Functions
    void set_active(bool s){
        is_active = s;
    }
    void set_moving(bool s){
        is_moving = s;
    }
    void end_switch(){
        is_end = !is_end;
    }
    void set_start(){
        g_start_state = id;
    }
    void set_x(int ix){
        x = ix;
    }
    void set_y(int iy){
        y = iy;
    }
    //Add functions
    void add_in_trans(int t){
        links_in.push_back(t);
    }
    void add_out_trans(int t){
        links_out.push_back(t);
    }
    //Delete Functions
    void remove_in_trans(){
        links_in.pop_back();
    }
    void remove_out_trans(){
        links_out.pop_back();
    }
    void draw_state();
    State(int ix, int iy);
    State(){}
};

State::State(int ix, int iy){
    id = g_state_num;
    if (g_start_state == 0)
        g_start_state = id;
    x = ix;
    y = iy;
}

void State::draw_state(){
    if (is_shown){
        if (is_moving)
            glTranslatef(g_cursor_x, g_cursor_y, 0.0);
        else
            glTranslatef(x, y, 0.0);
        fill_colour();
        if (is_active)
            active_fill_colour();
        glBegin(GL_POLYGON);
        for (size_t i=0; i<24; i++){
            float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
            glVertex2fv(n);
        }
        glEnd();
        line_colour();
        glBegin(GL_LINES);
        for (size_t i=0; i<24; i++){
            float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
            glVertex2fv(n);
        }
        glEnd();
        if(is_end){
            glPushMatrix();
            glScalef(0.9, 0.9, 0.9);
            for (size_t i=0; i<24; i++){
                float n[2] = {static_cast<float>(r * cos(i*6)), static_cast<float>(r * sin(i*6))};
                glVertex2fv(n);
            }
            glPopMatrix();
        }
        text_colour();
        std::string s = std::to_string(id);
        for (int i=0; i<s.length(); i++){
            glPushMatrix();
            glTranslatef(-radius/2 + i*kerning, -radius/2, 0.0);
            glScalef(0.3, 0.3, 1.0);
            glutStrokeCharacter(GLUT_STROKE_ROMAN, s[i]);
            glPopMatrix();
        }
    }
}

class Character{
    int id;
    char c;
public:
    int get_id(){
        return id;
    }
    char get_char(){
        return c;
    }
    void set_char(char ic){
        c = ic;
    }
    Character(char ic);
    Character(){};
};

Character::Character(char ic){
    id = g_character_num;
    g_character_num++;
    c = ic;
}

class Transition{
    int ident;
    State * from_state;
    State * to_state;
    float from[2];
    float to[2];
    Character c;
public:
    void set_from(float x, float y){
        from[0] = x;
        from[1] = y;
    }
    void set_to(float x, float y){
        to[0] = x;
        to[1] = y;
    }
    void set_char(Character ic){
        c = ic;
    }
    int get_id(){
        return ident;
    }
    void draw_trans();
    void set_trans(State * ifrom, State * ito, Character ic){
        from_state = ifrom;
        to_state = ito;
        from[0] = ifrom->get_x();
        from[1] = ifrom->get_y();
        to[0] = ito->get_x();
        to[1] = ito->get_y();
        c = ic;
    }
    Transition(){};
    Transition(State ifrom, State ito, Character ic){
        from_state = &ifrom;
        to_state = &ito;
        from[0] = ifrom.get_x();
        from[1] = ifrom.get_y();
        to[0] = ito.get_x();
        to[1] = ito.get_y();
        c = ic;
    }
};

void Transition::draw_trans(){
    line_colour();
    glBegin(GL_LINES);
    glVertex2fv(from);
    glVertex2fv(to);
    glEnd();
    float grad = (from[0] - to[0]) /(from[1] - to[1]); //(By finding the gradient of the slope, we can fin good place to show it's information, it's character.
    if (grad < -1 || grad > 1){
        glPushMatrix();
        glTranslatef(from[0] - to[0] - 20, from[1] - to[1], 1.0);
    }
else{
    glPushMatrix();
    glTranslatef(from[0] - to[0], from[1] - to[1] + 20, 1.0);
}
    glutStrokeCharacter(GLUT_STROKE_ROMAN, (c.get_char()));
    glPopMatrix();
}
#包括
阶级转型;
阶级国家
{
int-id;
std::矢量链接;
std::向量链接输出;
浮动x;
浮动y;
int r=radius;//x,y是表示状态的圆的中心坐标,而r是半径
bool为激活状态=错误;
bool是_end=false;
布尔值显示为真;
布尔在移动;
公众:
//获取函数
int get_x(){
返回x;
}
int get_y(){
返回y;
}
int get_id(){
返回id;
}
bool是否处于活动状态(){
返回处于活动状态;
}
布尔正在移动{
返回是移动的;
}
bool是(intix,intiy){//函数中的_,用于判断一对坐标是否在圆中,用于选择。

std::cerr在我看来,发布的代码还行。请发布一个。您可能会更好地使用智能指针,而不是哑指针。您的问题实际上可能与std::vector的使用无关,而是由于在其他地方破坏内存造成的。例如,转换ctor
转换(状态ifrom、状态ito、字符ic){from_state=&ifrom;to_state=&ito;[…]
看起来不太好。您获取的状态参数的地址立即超出范围。啊,是的,我明白您的意思。我已开始替换所有涉及其他已创建类的构造,以获取它们的智能指针,并解决了其他地方发生的一些向量问题,我的程序似乎我在工作,谢谢。