C++ C++;:构造函数错误中的向量赋值

C++ C++;:构造函数错误中的向量赋值,c++,vector,constructor,C++,Vector,Constructor,在我的课程中,我有以下课程: #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

在我的课程中,我有以下课程:

#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
红、绿、蓝是在另一个文件中定义的枚举。 对于我的整个程序,我在构建时没有错误。 当我想在main中创建类的实例时,例如:

Corners start;

当我运行程序时,它冻结并退出,错误为“分段错误”。我不知道为什么会这样,有人知道吗

你的向量没有容量。使用下标运算符时,向量不会自动调整大小。可以在colA和colB上调用resize()方法,并在设置值之前传入一个大于{RED,GREEN,BLUE}最大值的值。例如,如果蓝色是枚举中最大的:

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

向量的长度为0,因此无法指定位置。首先必须初始化它们的长度

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;
}
甚至像麋鹿建议的那样

Corners::Corners()
   : colA{5, 5, 5}
   , colB{31, 63, 31}
   , colC(colA)
   , colD(colB)
{}
。。这可以是内联的,就像他展示的那样

但也许您应该切换到
std::array
。 并使用
使用CustomArray=std::array
编辑:正如用户4581301所建议的,更好的是:

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

听起来您可能需要学习如何使用调试器逐步完成代码。有了一个好的调试器,您可以逐行执行您的程序,并查看它偏离预期的地方。这是一个必要的工具,如果你要做任何编程。进一步阅读:如何使用CustomVector=std::vector;)看来他需要
使用colorTuple=std::array
,然后是
std::array cols如果只有并且总是有3个元素,请考虑<代码> STD::数组< /代码>;但是,带有
红色
绿色
蓝色
成员的
类颜色
可能更容易阅读。您的代码不完整;特别是,它似乎缺少一个
main()
函数。请输入您的代码,使其成为您问题的一部分,然后我们可以尝试复制并解决它。您还应该阅读。您的成员初始化列表不必要地复杂,当您应该使用初始化构造函数时,您正在调用
vector
copy构造函数。您可以将列表简化为:
:colA(3),colB(3)
colC
colD
在主体中分配,因此不需要显式构造,并且没有声明
colE
Corners::Corners():colA{5,5,5},colB{31,63,31},colC(colD),colD(colD){/code>请注意
colA{5,5,5},COLB{31,63.3} <代码>只在C++ 11中工作,而List:是的,你是对的,但是C++已经6岁了…大多数现代编译器都支持它。谢谢,它解决了这个问题
struct Color {
    unsigned short int Red;
    unsigned short int Green;
    unsigned short int Blue;
};