0

I'm trying to get a little snippet of my code working, but it seems, that my array allocating is not working properly.I'd like t ask your expertise to pinpoint what's wrong.I've made a separate header and cpp file and a main cpp to fill up the array with the input the error.

the header file is 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

and the cpp file is 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] = ";";
}

so the main cpp is :

#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;
}}

the main calls for the void Nvalue/Texts which is supposed to fill the array with the inputs from other file but instead it dies with win error.

JohnnnnnY
  • 35
  • 9
  • You're so much better off with `std::vector`. – chris Dec 02 '12 at 14:02
  • 2
    Your default constructor doesn't allocate the array and your copy constructor doesn't do any copying. – Joseph Mansfield Dec 02 '12 at 14:02
  • What are `szertek` and `szoveg`? – Will A Dec 02 '12 at 14:02
  • show the main please. the problem can be that you start you instance with the default constructor which doesn't allocate the arrays. – Roee Gavirel Dec 02 '12 at 14:04
  • so i editetd it a little bit. the szertek and szoveg is the nvalue and texts just forgot to rewrite it, but i have corrected everything now hopefully and added main. – JohnnnnnY Dec 02 '12 at 14:48
  • so the only problem is the def constructor and copy constructor? could you please tell me how should i do it properly cause i tried googleing it, but i couldn't make it work so far. – JohnnnnnY Dec 02 '12 at 14:50

1 Answers1

1

You are not following the Rule of Three.
You need to provide a copy assignment operator and copy constructor which make deep copy of your dynamically allocated members.

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.

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533