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 ?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.