Crash while accessing specific index of vector in class

  c++, stdvector

I am currently trying to make a 2d vector representing pixels.
The problem is that when I try to set a value at a specific index the program crash.
The compilation is good.
Here is the .h :

#include "../include/graphics.h"
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <vector>
#include <string>

using namespace rgb_matrix;

class Viewport
{
public:
    Viewport(std::string id,int xMn,int yMn,int xMx,int yMx,Canvas *c)
    {
        canvas_=c;
        _id = id;
        _x = xMn;
        _y = yMn;
        width=xMx-xMn;
        height=yMx-yMn;

    }

    void SetPixel(int x_vport,int y_vport,rgb_matrix::Color c);
    int getXMax(){
        return _x + height;
    }
    int getHeight(){
        return height;
    }
    int getWidth(){
        return width;
    }
    int getX(){
        return _x;
    }
    void clear();
    std::string getId(){return _id;}
    void show();
    void fill(int r,int g,int b);
private :
    Canvas *canvas_;
    int width,height;
    int _x,_y;
    std::string _id;
    std::vector<std::vector<rgb_matrix::Color> > colors;
};

The weird point is that I can access to the values into the .h but not into the .cpp :

void Viewport::SetPixel(int x, int y, rgb_matrix::Color c) {
    if(x>=0 && y>= 0 && x< width && y < height) {//if in rectangle
        colors.at(x).at(y) = c;
    }
}

Source: Windows Questions C++

LEAVE A COMMENT