0

我正试图让我的代码的一小段工作,但似乎我的数组分配工作不正常。我不想问你的专业知识来查明问题所在。我已经制作了一个单独的头文件和 cpp 文件和一个主 cpp 用输入错误填充数组。

头文件是 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 错误而死。

4

1 回答 1

1

你没有遵循三法则
您需要提供一个复制赋值运算符复制构造函数,它们可以对动态分配的成员进行深层复制。

On a honest note probably, You are much better of using std::vector as member than dynamic memory allocated members.

Also, make sure you are allocating dynamic memory in every constructor which can construct an object. In the code shown the constructor which takes no arguments simply doesnt allocate any memory to pointer members.

于 2012-12-02T14:02:44.410 回答