0

I want to make a dynamically allocated 2d array, but I don't really know how to do it. I've searched google and everything, but just couldn't make it work. My code so far is:

postaH.h:

#ifndef POSTAHH
#define POSTAHH
#include <vector>
#include <string>
#include <iostream>
#include "feladoH.h"

using namespace std;

class postaH : public feladoH{
  private:
          int szertekek[7];
          string szovegek[7];
          int a,b;
          int** szamosszesit;
          string d,c;
          string** szovosszesit;
  public:
         postaH();
         postaH(int szertekek[7], string szovegek[7],int,int,string,string);
         ~postaH();
         postaH(postaH& pos);
         int getSzertek();
         string getSzovegek();
         void setSzertek(int ir,int hsz,int szulev, int cir, int chsz,int surg, int suly);
         void setSzovegek(string nev,string varos,string utca,string cnev,string cvaros,string cutca,string cstipus);
         void hozzaad();
         void fajlbakiir();
};
#endif

postaS.cpp:

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

int seged = 0;

using namespace std;

postaH::postaH(){
          int szertekek[7];
          string szovegek[7];     
          int** szamosszesit;
          string** szovosszesit;
   }

postaH::postaH(int szertekek[7], string szovegek[7],int aa,int bb,string dd,string cc)     : a(aa),b(bb),d(dd),c(cc){
   this->szertekek[7] = szertekek[7];
   this->szovegek[7] = szovegek[7];
    szamosszesit = new int*[a];
for (int i = 0; i<a; i++){
        szamosszesit[i] = new int[b];
    }
    for (int i = 0; i<a; i++){
    for (int j = 0; j<b; j++){
        szamosszesit[i][j] = 0;
    }
szovosszesit = new string*[d];
for (int i = 0; i<d; i++){
    szovosszesit[i] = new string[d];
}
for (int i = 0; i<d; i++){
    for (int j = 0; j<c; j++){
        szovosszesit[i][j] = 0;
        }
}

postaH::~postaH(){
              delete [] szertekek;
              delete [] szovegek;
              for (int i = 0; i<a; i++){
                  delete [] szamosszesit[i];
                    } 
                    delete [] szamosszesit;
            for (int i = 0; i<d; i++){
                  delete [] szovosszesit[i];
                    } 
                        delete [] szovosszesit;
}

postaH::postaH(postaH& pos){
   this->szertekek[7] = pos.getSzertek();
   this->szovegek[7] = pos.getSzovegek(); 
    }

int postaH::getSzertek(){
return szertekek[7];
}

std::string postaH::getSzovegek(){
return szovegek[7];        
}

void postaH::setSzertek(int ir,int hsz,int szulev, int cir, int chsz,int surg, int suly){
 this->szertekek[0] = ir;
 this->szertekek[1] = hsz;
 this->szertekek[2] = szulev;
 this->szertekek[3] = cir;
 this->szertekek[4] = chsz;
 this->szertekek[5] = surg;
 this->szertekek[6] = suly;
}

void postaH::setSzovegek(string nev,string varos,string utca,string cnev,string cvaros,string cutca,string cstipus){
 this->szovegek[0] = nev;
 this->szovegek[1] = varos;
 this->szovegek[2] = utca;
 this->szovegek[3] = cnev;
 this->szovegek[4] = cvaros;
 this->szovegek[5] = cutca;
 this->szovegek[6] = cstipus;
}

void postaH::hozzaad(){

}

void postaH::fajlbakiir(){
 ofstream kezb;
 kezb.open ("kezbesitett.txt", ios::app);
 for( int i=0;i<7;i++){
      kezb << szovegek[i] << ",";
      kezb << szertekek[i] << ",";
      }
 kezb << "\n";
 kezb.close();
}

So my question is basically how should I make the declaration in the header file, and the constructors, destructor in the cpp file to work ?

4
  • 1
    Try from the very beginning to code "in English", those Hungarian function and variable names are not very readable. Commented Nov 30, 2012 at 11:46
  • basically i want to make 2 2d array, to put some of the stored data in it. the user gives data trough console, which is stored in the szertekek,szovegek array, and i want to store that data in a 2d array(szovosszesit,szamosszesit) so i can print the last 10 stored data. Commented Nov 30, 2012 at 11:47
  • 1
    A lot of context can be gleaned from the function and variable names. However since the general audiance cannot read Hungarian that context is lost. Commented Nov 30, 2012 at 11:56
  • 1
    I highly recommend against using "dynamic arrays" using pointers. A std::vector of vectors is much better. E.g. std::vector<std::vector<int>> szamosszesit; Commented Nov 30, 2012 at 11:57

1 Answer 1

2

Generally if you are creating an array it is better to use a container suited to that task:

  • std::vector for a sequential container of items.
  • std::deque for a sequential container of items that needs updating from either end.
  • std::list for a sequential container that will be updated from any point in the sequence.
Sign up to request clarification or add additional context in comments.

1 Comment

My definition of sequential is probably different from the mathematical. I am using it to describe that you can easily enumerate from [0..n].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.