简体   繁体   中英

C++ : Vector assignment in constructor error

in my program i Have following class:

#ifndef CORNERS_H
#define CORNERS_H

#include <vector>

class Corners{
public:
    Corners();
    Corners( std::vector<unsigned short int>,std::vector<unsigned short int> );
    Corners( std::vector<unsigned short int>,std::vector<unsigned short int>,
            std::vector<unsigned short int>, std::vector<unsigned short int> );

    void set2Corners( std::vector<unsigned short int>,std::vector<unsigned short int> );
    void set4Corners( std::vector<unsigned short int>,std::vector<unsigned short int>,
            std::vector<unsigned short int>, std::vector<unsigned short int> );

    std::vector<unsigned short int> getA();
    std::vector<unsigned short int> getB();
    std::vector<unsigned short int> getC();
    std::vector<unsigned short int> getD();


private:
    std::vector<unsigned short int> colA;
    std::vector<unsigned short int> colB;
    std::vector<unsigned short int> colC;
    std::vector<unsigned short int> colD;
};

#endif // CORNERS_H

Default constructor for this class looks like this:

Corners::Corners(){

    colA[RED] = 5;
    colA[GREEN] = 5;
    colA[BLUE] = 5;

    colB[RED] = 31;
    colB[GREEN] = 63;
    colB[BLUE] = 31;

    colC=colA;
    colD=colB;

}

RED GREEN BLUE are enums defined in another file. For my whole program, I get no errors when building. When i want to make instance of the class in my main, for example:

Corners start;

When I run the program, it freezes and quits with error "Segmentation fault".I have no idea why it is so, does anyone has any idea?

the vectors are length 0, so you cannot assign to locations. You first have to initialize their length.

Corners::Corners()
   : colA(3)
   , colB(3)
   , colC(3)
   , colD(3)
{
    colA[RED] = 5;
    colA[GREEN] = 5;
    colA[BLUE] = 5;

    colB[RED] = 31;
    colB[GREEN] = 63;
    colB[BLUE] = 31;

    colC=colA;
    colD=colB;
}

Or even like Mooing Duck suggested.

Corners::Corners()
   : colA{5, 5, 5}
   , colB{31, 63, 31}
   , colC(colA)
   , colD(colB)
{}

.. which could be inlined, like he shows.

but maybe you should switch to std::array<unsigned short int, 3> . And use using CustomArray=std::array<unsigned short int, 3>; so you don't have to write that long definition every time.

edit: and as user4581301 suggests, even better is:

struct Color {
    unsigned short int Red;
    unsigned short int Green;
    unsigned short int Blue;
};

Your vectors have no capacity. Vectors don't automatically resize when using the subscript operator. You can call the resize() method on colA and colB, and pass in one greater than the max of {RED,GREEN,BLUE} before setting the values. For example, if BLUE is the largest in your enum:

Corners::Corners() {
    colA.resize(BLUE + 1);
    colB.resize(BLUE + 1);

    colA[RED] = 5;
    colA[GREEN] = 5;
    colA[BLUE] = 5;

    colB[RED] = 31;
    colB[GREEN] = 63;
    colB[BLUE] = 31;

    colC=colA;
    colD=colB;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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