不同类的成员列表实现不起作用

时间:2014-11-09 10:59:52

标签: c++ oop constructor initialization member

我似乎无法正确执行我的成员列表。我想DEFAULT初始化我的Set成员 nyX nyY ,但是我一直收到错误。

class Location
{
public:

    vector<int> nyXv = { 0, 1, 2, 3, 4, 5};
    vector<int> nyYv = { 0, 1, 2, 3, 4, 5 };

    Set nyX(vector<int>);
    Set nyY(vector<int>);

    Location();
    ~Location();

    };


    Location::Location()
        :nyX(nyXv), nyY(nyYv)
    {
    }

1 个答案:

答案 0 :(得分:0)

Look at this example

您可以初始化这样的矢量:

class Location
{
    public:

    vector<int> nyXv;// = { 0, 1, 2, 3, 4, 5};
    vector<int> nyYv;// = { 0, 1, 2, 3, 4, 5 };

    ///...

    Location();
    ~Location();

};


static const int arrX[] = {0, 1, 2, 3, 4, 5};
static const int arrY[] = {0, 1, 2, 3, 4, 5};
Location::Location()
   :nyXv(arrX, arrX + sizeof(arrX) / sizeof(arrX[0]) )
   ,nyYv(arrY, arrY + sizeof(arrY) / sizeof(arrY[0]))
{
}

P.S。当然,有很多方法可以改进这个代码,但它应该给你一个想法