2012-07-05 55 views
3

我正在構建一個類,其中一個變量是一個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_ */ 
+1

vector < vector> pncode(5,vector (5)); – CapelliC 2012-07-05 18:19:18

+0

空出>符號 – user1084113 2012-07-05 18:22:32

+0

vector < vector> pncode(5,vector (5));仍然得到「預期」,或'...'在數字常量之前「 – postelrich 2012-07-05 18:27:19

回答

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 &,並消除了自由手動複製矢量元素。

+0

謝謝你修復這個問題,但現在當我嘗試一個名爲pcodes,pcodes.push_back(postnet(pnc))的postnet矢量;從主要我得到沒有匹配的函數的調用。這是將pnc設置爲矢量> pnc(5,vector (5)); – postelrich 2012-07-05 19:56:32

+0

@riotburn我不明白爲什麼會這樣。鑑於我所顯示的變化,這對我來說編譯得很好:'vector pcodes;矢量> pnc(5,vector (5)); pcodes.push_back(postnet(pnc));' – bames53 2012-07-05 20:47:03

+0

@ barnes53我試着重新啓動Eclipse,現在編譯。 Eclipse解決更改速度很慢。謝謝你的幫助! – postelrich 2012-07-06 01:23:45