我正在構建一個類,其中一個變量是一個2d向量。我如何設置它,以便我有一個初始大小?我嘗試了下面的內容,但在數字常量之前得到一個錯誤,預計','或'...'。另外我得到每一行的錯誤,我嘗試設置一個值爲pncode,數組下標爲無效類型'[int]'。將2d向量初始化爲類變量C++
這裏的類:
#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_ */
vector < vector> pncode(5,vector (5)); –
CapelliC
2012-07-05 18:19:18
空出>符號 – user1084113 2012-07-05 18:22:32
vector < vector> pncode(5,vector (5));仍然得到「預期」,或'...'在數字常量之前「 –
postelrich
2012-07-05 18:27:19