将2d向量初始化为类变量C ++

时间:2012-07-05 18:12:51

标签: c++ class vector

我正在构建一个类,其中一个变量是一个二维向量。如何设置它以便我有一个初始大小?我尝试了以下但是在数字常量之前得到错误,预期','或'...'。另外,我尝试将值设置为pncode的每一行都会出错,对于数组下标,无效类型'[int]'。

这是班级:

#ifndef POSTNET_H_
#define POSTNET_H_

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;

class postnet
{
private:
    vector<vector<int>> pncode(5, vector<int>(5));
    void encode(vector<int> zip)
    {
        for(int i = 0; i < 5; i++)
        {
            if(zip[i] == 1 || zip[i] == 2 || zip[i] == 3 || zip[i] == 4 || zip[i] == 5 || zip[i] == 6)
                pncode[i][0] = 0;
            else
                pncode[i][0] = 1;

            if(zip[i] == 1 || zip[i] == 2 || zip[i] == 3 || zip[i] == 7 || zip[i] == 8 || zip[i] == 9)
                pncode[i][1] = 0;
            else
                pncode[i][1] = 1;
            if(zip[i] == 1 || zip[i] == 4 || zip[i] == 5 || zip[i] == 7 || zip[i] == 8 || zip[i] == 0)
                pncode[i][0] = 0;
            else
                pncode[i][0] = 1;
            if(zip[i] == 2 || zip[i] == 4 || zip[i] == 6 || zip[i] == 7 || zip[i] == 9 || zip[i] == 0)
                pncode[i][0] = 0;
            else
                pncode[i][0] = 1;
            if(zip[i] == 3 || zip[i] == 5 || zip[i] == 6 || zip[i] == 8 || zip[i] == 9 || zip[i] == 0)
                pncode[i][0] = 0;
            else
                pncode[i][0] = 1;
        }
    }
    void decode(vector<int> zip)
    {
        int zipd[5];

        for(int i = 0; i < 5; i++)
        {
            zipd[0] = pncode[i][0] * 7;
            zipd[1] = pncode[i][1] * 4;
            zipd[2] = pncode[i][2] * 2;
            zipd[3] = pncode[i][3] * 1;
            zipd[4] = pncode[i][4] * 0;

            for(int j = 0; j < 5; j++)
            {
                zip[i] += zipd[j];
            }
            if(zip[i] == 11)
                zip[i] = 0;
        }
    }

public:
    postnet()
    {
        for(int i = 0; i < 5; i++)
            for(int j = 0; j < 5; j++)
                pncode[i][j] = 0;
    }
    postnet(vector<int> zip)
    {
        encode(zip);
    }
    postnet(vector<vector<int> > pnc)
    {
        for(int i = 0; i < 5; i++)
            for(int j = 0; j < 5; j++)
                pncode[i][j] = pnc[i][j];
    }
    void print_zip()
    {
        vector<int> zipc(5);
        decode(zipc);

        for(int i = 0; i < 5; i++)
            cout << zipc[i];

        cout << endl;
    }

    void print_pncode()
    {
        cout << "1";
        for(int i = 0; i < 5; i++)
            for(int j = 0; j < 5; j++)
                cout << pncode[i][j];
        cout << "1" << endl;
    }

};


#endif /* POSTNET_H_ */

1 个答案:

答案 0 :(得分:4)

现在的代码vector<vector<int>> pncode(5, vector<int>(5));足以满足C ++ 11编译器的要求。在此之前,您必须在构造函数中初始化pncode。 (您可能还需要在尖括号之间添加空格; >>。)

postnet(vector<int> const &zip)
    : pncode(5, vector<int>(5))
{
    encode(zip);
}
postnet(vector<vector<int> > const &pnc = vector<vector<int> >(5, vector<int>(5)))
{
    assert(pnc.size()==5);
    for (int i=0;i<pnc.size();++i)
        assert(pnc[i].size()==5);

    pncode = pnc;
}

我还冒昧地用默认参数替换默认构造函数,将参数类型更改为const&amp;和消除向量元素的手动复制。