You're not showing the relevant code, but since you said "calling a destructor", my response is: DON'T.
Simply don't manually call a destructor. The point of a destructor is to be automatically called when a statically allocated object goes out of scope, or when delete is called on an new'd (dynamically allocated) object.
#include <iostream>
usingnamespace std;
#include"Zoo.h"
#include"DugackiBroj.h"
int main()
{
int a;
int *p;
try {//I would add more functions in try block later this is a beginning
Zoo<int> z1(4);
Zoo<DugackiBroj> z2(4);
for (int i = 0; i < 4; i++)
{
cout << "Unesite ceo broj i dugacak broj" << endl;
cin >> a;
DugackiBroj pom(a);
cin >> pom;
z1.Dodaj(a);
z2.Dodaj(pom);
}//after this block error
}
catch (constchar * error)
{
cerr << "An error has occured -" << error << endl;
}
return 0;
}
#pragma once
#include<iostream>
#include <fstream>
usingnamespace std;
template<class T>
class Zoo
{
int tragac;
int d;
T *niz;
public:
Zoo(int a);
~Zoo();
void Dodaj(T a);
void Ukloni();
T Suma(int a, int b);
void Snimi_u_fajl(char * c);
void Ucitaj_iz_fajla(char * c);
};
template<class T>
inline Zoo<T>::Zoo(int a)
{
d = a;
tragac = 0;
niz = new T[d];
}
template<class T>
inline Zoo<T>::~Zoo()
{
delete[] niz;
}
template<class T>
inlinevoid Zoo<T>::Dodaj(T a)
{
if (tragac == d)
{
T *pom;
pom = new T[d + d];
for (int i = 0; i<d; i++)
pom[d+d-i-1] = niz[d-i-1];
delete[] niz;
niz = pom;
d = 2 * d;
for (int i = d - tragac; i < d; i++)
niz[i - 1] = niz[i];
niz[d - 1] = a;
//Predefinisati operator =
tragac++;
}
for (int i = d - tragac; i < d; i++)
niz[i - 1] = niz[i];
niz[d-1] = a;
tragac++;
}
template<class T>
inlinevoid Zoo<T>::Ukloni()
{
if (tragac == 0)
throw"Niz je prazan";
T * pom;
pom = new T[d - 1];
for (int i = 0; i < d - 1; i++)
pom[i] = niz[i];
delete[] niz;
niz = pom;
--d;
--tragac;
}
template<class T>
inline T Zoo<T>::Suma(int a, int b)
{
T pom = T();
for (int i = a; i < b; i++)
pom = pom + niz[i];// predefinisati operator +
return pom;
}
template<class T>
inlinevoid Zoo<T>::Snimi_u_fajl(char * c)
{
ofstream f(c);
if (!f.good)
{
throw"Nije se dobro otvorio fajl";
f.close();
}
for (int i = d - tragac; i < d; i++)
f << niz[i];
f.close();
}
template<class T>
inlinevoid Zoo<T>::Ucitaj_iz_fajla(char * c)
{
ifstream f(c);
if (!f.good)
{
throw"Nije se dobro otvorio fajl";
f.close();
}
f >> d;
if (!f.good)
{
throw"Nije se dobro ucitao element";
f.close();
}
int i = 0;
while (i < d)
f >> niz[d - i];
f.close();
}
DugackBroj.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#pragma once
#include<istream>
usingnamespace std;
class DugackiBroj
{
int popunj;
int f;
int * broj;
public:
DugackiBroj() { popunj = 0; f = 0; broj = nullptr; }
DugackiBroj(int a);
DugackiBroj(const DugackiBroj &b);
~DugackiBroj();
friend istream& operator>>(istream &in, DugackiBroj & b);
friend ostream& operator<<(ostream &out,const DugackiBroj & b);
DugackiBroj& operator=(const DugackiBroj &b);
friend DugackiBroj operator+(const DugackiBroj &b1, const DugackiBroj &b2);
};
You're welcome. There might be other issues beyond that, I didn't dig too far in.
Yes, the && operator will do what's known as "short-circuiting" and prevent the second test from being executed if the first test fails.
As soon as you try to put information into an invalid index in b.broj[i], your program is invalid, so it has to be prevented from happening in the first place.