Debug error

Hi, this is my first post here and I already have something to ask if anyone can help me. I must do this program that has all the stuff that we are doing in it so i can go to an exam. The problem is that when I'm asked if i want to continue writing the Film (Movie) data error happens. It says that the error is after block 138 but i really cant find it.

this is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include<iostream>
#include<string>
#include<exception>
#include<vector>

using namespace std;

class izuzetak : public exception{
	int linijaKoda;
public:
	izuzetak(char * poruka, int linijaKoda):exception(poruka){
		this->linijaKoda = linijaKoda;
	}
	void ErrorInfo(){
		cout<<"Greska: "<<exception::what()<<" linija koda: "<<linijaKoda<<endl;
	}
};

class Fajlovi{
protected:
	char * Naziv;
	char * filetype;
	float trajanje;
	//int resWidth;
	//int resHeight;
public:
	Fajlovi(char * Naziv, char * ft, float Tr){
		this->Naziv = new char[strlen(Naziv)+1];
		strcpy(this->Naziv,Naziv);
		filetype = new char[strlen(ft)];
		strcpy(filetype,ft);
		if(Tr <= 0)
			throw exception("Trajanje ne moze biti manje ili jednako nula!! ");
		trajanje = Tr;
		//resWidth = rW;
		//resHeight = rH;
	}

	virtual ~Fajlovi(){
		//cout<<"Pozvan destruktor za: "<<filetype<<endl;
		delete [] Naziv;
		delete [] filetype;
		Naziv = NULL;
		filetype = NULL;
	}

	virtual void Info() = 0;
};

class Film : virtual public Fajlovi{
	char * Reziser;
	string Zanr;
	string Distributor;
	//int trajanjeFilma;
public:
	Film(char * Naziv, char * filetype, float trajanje, char * reziser, string Zanr, string Distributor) : Fajlovi(Naziv,filetype,trajanje){
		Reziser = new char[strlen(reziser)+1];
		strcpy(Reziser,reziser);
		this->Zanr = Zanr;
		this->Distributor = Distributor;
	}
	~Film(){
		//cout<<"Pozvan destruktor za: "<<Reziser<<endl;
		delete [] Reziser;
		Reziser = NULL;
	}

	friend ostream & operator << (ostream & out, Film & obj){
		out<<"Naziv Filma: "<<obj.Naziv<<endl;
		out<<"Reziser: "<<obj.Reziser<<endl;
		out<<"Zanr: "<<obj.Zanr<<endl;
		out<<"Distributer: "<<obj.Distributor<<endl;
		out<<"Trajanje: "<<obj.trajanje<<endl;
		out<<"Tip fajla: "<<obj.filetype<<endl;

		return out;
	}

	void Info(){
		cout<<*this<<endl;
	}

};

class Muzika : virtual public Fajlovi{
	char * izvodjac;
	char * tipMuzike;
public:
	Muzika(char * Naziv, char * filetype, float trajanje, char * Izvodjac, char * tipM):Fajlovi(Naziv,filetype,trajanje){
		//if(strlen(Izvodjac)<1)
			//throw exception("Pjesma mora imati izvodjaca! ");
		izvodjac = new char[strlen(Izvodjac)+1];
		strcpy(izvodjac,Izvodjac);
		tipMuzike = new char[strlen(tipM)+1];
		strcpy(tipMuzike,tipM);
	}
	~Muzika(){
		delete [] izvodjac;
		delete [] tipMuzike;
		izvodjac = NULL;
		tipMuzike = NULL;
	}

	friend ostream & operator <<(ostream & out,Muzika & obj){
		out<<"Naziv pjesme: "<<obj.Naziv<<endl;
		out<<"Izvodjac: "<<obj.izvodjac<<endl;
		out<<"Vrsta: "<<obj.tipMuzike<<endl;
		out<<"Trajanje pjesme: "<<obj.trajanje<<" min. "<<endl;
		out<<"Tip fajla: "<<obj.filetype<<endl;
		
		return out;
	}
	void Info(){
		cout<<*this<<endl;
	}
};

string ln="\n--------------------------------------------------------------------\n";

void izlistaj(vector<Film> filmovi)
{
	cout<<ln;
	for(int i=0; i<filmovi.size();i++)
		cout<<"\t"<<i<<". "<<filmovi[i]<<endl;
	cout<<ln;
}

void izbrisi(vector<Film> & filmovi)
{
	int pocetak=0, kraj=0;
	cout<<ln<<"Brisanje filmova"<<ln;
	for(int i=0;i<filmovi.size();i++)
		cout<<"\t"<<i<<". "<<filmovi[i]<<endl;
	cout<<ln<<"Unesite pocetnu i krajnju lokaciju koju zelite izbrisati: ";
	cin>>pocetak>>kraj;
	filmovi.erase(filmovi.begin()+pocetak,filmovi.begin()+kraj);
}



void main(){
	char  Naziv[50];
	char  filetype[50];
	float trajanje=0;
	char  reziser[50];
	string zanr, distributer;
	char nastavak;
	vector<Film> filmovi;
	try{
	do{
		cout<<ln<<"Unesite naziv filma: ";
		cin.getline(Naziv,49);
		//cin.getline(Naziv,50);
		cout<<"\nUnesite tip fajla: ";
		cin.getline(filetype,49);
		cout<<"\nReziser filma: ";
		cin.getline(reziser,49);
		cout<<"\nZanr: ";
		getline(cin,zanr);
		cout<<"\nDistributer: ";
		getline(cin,distributer);
		cout<<"\nTrajanje filma: ";
		cin>>trajanje;

		Film temp(Naziv,filetype,trajanje,reziser,zanr,distributer);
		filmovi.push_back(temp);

		cout<<"Da li zelite nastaviti (d/n): ";
		
		cin>>nastavak;
		if(nastavak == 'n')
			break;

	}while(1);
	}

	catch(exception & a)
	{
		cout<<"Greska!\nDetalji: "<<a.what()<<endl;
	}

	system("cls");
	cout<<ln<<"Filmovi (prije brisanja): ";
	izlistaj(filmovi);

	izbrisi(filmovi);

	cout<<ln<<"Filmovi (posle brisanja): ";
	izlistaj(filmovi);


	system("pause");
}
Last edited on
Many comments.

First, why oh why are you using virtual inheritance?

Here's some of your code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Fajlovi
{
protected:
	char * Naziv;
	char * filetype;
	float trajanje;
	//int resWidth;
	//int resHeight;

public:
	Fajlovi(char * Naziv, char * ft, float Tr)
	{
		this->Naziv = new char[strlen(Naziv)+1];
		strcpy(this->Naziv,Naziv);
		filetype = new char[strlen(ft)];
		strcpy(filetype,ft);
		if(Tr <= 0)
			throw exception("Trajanje ne moze biti manje ili jednako nula!! ");
		trajanje = Tr;
		//resWidth = rW;
		//resHeight = rH;
	}


Why do you have a member and parameter named Naziv?

filetype isn't large enough, which causes strcpy to overwrite the end.

Why aren't using using std::string for strings?

The program stops because of the exception you throw here.
Last edited on
I must use all the things we used on our classes, that includes virtual inheritance. When I write my program with this

/*try{
Film * Prvi = new Film("Godfellas","avi",120,"Dzenan Sljivo","Crime","Universal Pictures");
Muzika * Drugi = new Muzika("Beat It","mp3",4.5,"Michael Jackson ","Rock");

//cout<<*Prvi<<endl;
Prvi->Info();
Drugi->Info();
//cout<<*Drugi<<endl;

}
catch(exception & a)
{
cout<<"Greska!\nDetalji: "<<a.what()<<endl;
}*/


Instead of that menu (I have to make menu for inserting and deleting information) it works fine, and it reports exception if I enter 0 for song.

Naziv is Name of the file because it can be used for a song or movie (Muzika, filmovi). And I used it because I want to show that I can use pointer this if I have same situation on exam.

Last edited on
closed account (z05DSL3A)
My guess is that Film temp(Naziv,filetype,trajanje,reziser,zanr,distributer); is corrupting the heap, but I'm having trouble reading through the code.

Could you edit your post and put code tags in?
[code]
Your code
[/code]
There you go, I've edited the post.

Now I did this

1
2
3
Film * temp = new Film(Naziv,filetype,trajanje,reziser,zanr,distributer);
		filmovi.push_back(*temp);


and now I can choose to continue inserting the data but it isn't OK as you can see on the pic

http://img269.imageshack.us/img269/4773/cmd2.jpg
Last edited on
closed account (z05DSL3A)
As kbw said, Line 30 filetype = new char[strlen(ft)]; needs more room:
filetype = new char[strlen(ft)+1];

If you have further problems, it maybe down to the lack of a copy constructor to do deep copies of you pointers data.
Last edited on
Topic archived. No new replies allowed.