2012-12-02 85 views
0

我試圖讓我的代碼工作的一小段,但它似乎,我的數組分配不能正常工作。我想問你的專業知識,以查明什麼是錯的。我做了一個單獨的頭文件和cpp文件以及一個主cpp來填充輸入錯誤的數組。C++數組問題

頭文件是postaH.h:

#ifndef POST_H 
#define POST_H 
#include <string> 
#include <iostream> 
#include "senderH.h" 
using namespace std; 
class postH : virtual public senderH{ 
     private: 
       int* nvalue; 
       string* texts; 
     public: 
      postH(); 
      virtual ~postH(); 
      postH(postH&); 
      void Nvalue(int,int,int, int, int,int, int); 
      void Texts(string,string,string,string,string,string,string); 
}; 
#endif 

和cpp文件是postaS.cpp:

#include <string> 
#include <iostream> 
#include <fstream> 
#include "postH.h" 


using namespace std; 

postH::postH(){ 
} 

postH::postH(postH& pos){ 
    nvalue = new int[8]; 
    texts = new string[8]; 
} 

postH::~postH(){ 
        delete [] nvalue; 
        delete [] texts; 
} 

void postH::Nvalue(int ir,int hsz,int szulev, int cir, int chsz,int surg, int suly){ 
    nvalue[0] = ir; 
    nvalue[1] = hsz; 
    nvalue[2] = szulev; 
    nvalue[3] = cir; 
    nvalue[4] = chsz; 
    nvalue[5] = surg; 
    nvalue[6] = suly; 
    nvalue[7] = 0; 
} 

void postH::Texts(string nev,string varos,string utca,string cnev,string  cvaros,string cutca,string cstipus){ 
    texts[0] = nev; 
    texts[1] = varos; 
    texts[2] = utca; 
    texts[3] = cnev; 
    texts[4] = cvaros; 
    texts[5] = cutca; 
    texts[6] = cstipus; 
    texts[7] = ";"; 
} 

所以主CPP是:

#include<iostream> 
#include<string.h> 
#include<stdio.h> 
#include "senderH.h" 
#include "postH.h" 
using namespace std; 

int main() { 
    postH post1; 
    senderH send1; 
    int b; 
    string input,a,quit = "q",inp1="package",inp2="alrsent"; 
    while(input < quit){ 
    cout<<"\nwelcome to the automated post office!!\n"<<endl; 
    cout<<"For starting a send procedure please type |package|! For a list of sent  mails/packages type |alrsent|:"; cin >> feladat; 
    if(input == inp1) { 
     cout<<"Nev: "; cin >> a; 
     send1.setNev(a); 
     cout<<"Varos: "; cin >> a; 
     send1.setVaros(a); 
     cout<<"Utca: "; cin >> a; 
     send1.setUtca(a); 
     cout<<"IR_szam: "; cin >> b; 
     send1.setIR_szam(b); 
     cout<<"Hazszam: "; cin >> b; 
     send1.setHazszam(b); 
     cout<<"Szul_ev: "; cin >> b; 
     send1.setSzul_ev(b); 
     cout<<"Cimzett Nev: "; cin >> a; 
     send1.setC_Nev(a); 
     cout<<"Cimzett IR_szam: "; cin >> b; 
     send1.setC_IR(b); 
     cout<<"Cimzett Varos: "; cin >> a; 
     send1.setC_Varos(a); 
     cout<<"Cimzett Utca: "; cin >> a; 
     send1.setC_Utca(a); 
     cout<<"Cimzett Hazszam: "; cin >> b; 
     send1.setC_Hazszam(b); 
     cout<<"Csomag tipus: "; cin >> a; 
     send1.setCS_Tipus(a); 
     cout<<"Csomag Surgosseg: "; cin >> b; 
     send1.setSurgosseg(b); 
     cout<<"Csomag Suly: "; cin >> b; 
     send1.setCS_Suly(b); 

      post1.Nvalue(send1.getIR_szam(),send1.getHazszam(),send1.getSzul_ev(),send1.getC_IR(),send1 .getC_Hazszam(),send1.getSurgosseg(),send1.getCS_Suly()); 
       } 
    system("PAUSE"); 
    return 0; 
}} 

主要求使用來自其他文件的輸入來填充陣列的void Nvalue/Texts,但是它會因爲win錯誤而死亡。

+0

你用'std :: vector'好多了。 – chris

+2

您的默認構造函數不會分配數組,您的複製構造函數不會執行任何複製。 –

+0

什麼是szertek和szoveg? –

回答

1

您沒有關注Rule of Three
您需要提供一個複製分配運算符複製構造函數,它們使您的動態分配成員的深層副本。

說實話,你可以更好地使用std::vector作爲動態內存分配成員的成員。

此外,請確保您分配動態內存在每個構造函數可以構造一個對象。在顯示的代碼中,不帶參數的構造函數不會爲指針成員分配任何內存。