Crasg

Hey guys, I hope you can help me.

I wrote the following programm:

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
//Main

#include<iostream>
#include "Bilddaten.h"

using namespace std;

int main()
{
	Bilddaten Objekt;
	Objekt.Eingabe();
	Objekt.Ausgabe1();
	Objekt.Ausgabe2();
	
	system("pause");
	return 0;
}

//Header

#pragma once
#include <iostream>

using namespace std;

#ifndef Bilddaten_H
#define Bilddaten_H

class Bilddaten
{
public:
	// default constructor
	Bilddaten();

	// overload constructor
	Bilddaten(int, int);

	// destructor
	~Bilddaten();

	//accesor function
	void Eingabe();
	int getIndex(int x, int y, int H);
	int getX(int i, int H);
	int getY(int i, int H);	
	void Ausgabe1();
	void Ausgabe2();

private:
	int H;
	int V;
	int C = H * V;
	int* Field = new int[C];	//erzeugt ein dynamisches Feld
	int counter = 0;
	int x, y, i;
};

#endif


//source

#include "Bilddaten.h"

Bilddaten::Bilddaten()
{
	int H = 0;
	int V = 0;
}

Bilddaten::Bilddaten(int neuH, int neuV)
{
	int H = neuH;
	int V = neuV;
}

Bilddaten::~Bilddaten()
{
	delete[] Field;
}

void Bilddaten::Eingabe()
{
	cout << "Breite eingeben: ";
	cin >> H;
	cout << endl;
	cout << "Hoehe eingeben: ";
	cin >> V;
	cout << endl;
}

int Bilddaten::getIndex(int x, int y, int H)
{
	int i;
	i = y * H + x;
	return i;
}

int Bilddaten::getX(int i, int H)
{
	int x = i % H;
	return x;
}

int Bilddaten::getY(int i, int H)
{
	int y = i / H;
	return y;
}

void Bilddaten::Ausgabe1()
{
	for (int y = 0;y < V;y++)
	{
		for (int x = 0;x < H;x++)
		{
			cout << " (" << y << " , " << x << ") " << counter;
			Field[getIndex(x, y, H)] = counter++;
		}
		cout << endl;
	}
	cout << endl;
}

void Bilddaten::Ausgabe2()
{
	for (int i = 0;i < C;i++)
	{
		x = getX(i, H);
		y = getY(i, H);
		cout << Field[i] << "-->" << x << "," << y << "\t";
	}
	cout << endl;
	cout << endl;
	cout << H << "	" << V << "	" << x << "	" << y << "	" << i << endl;
}
  


When the programm comes to the function Ausgabe2() it crasches and I dont know why. I asume there is an issue with the dynamic array. Can you help me to fix it?
Hi Vitalik2, what do you think is happening here?
1
2
3
4
5
6
7
8
9
10
11
Bilddaten::Bilddaten()
{
	int H = 0;
	int V = 0;
}

Bilddaten::Bilddaten(int neuH, int neuV)
{
	int H = neuH;
	int V = neuV;
}
In the constructors you are creating and initializing two local variables name H and V that are not the same as the member variables with the same names.

If you want to assign values to the member variables you need to remove the type in front of them.

1
2
3
4
5
6
7
8
9
10
11
Bilddaten::Bilddaten()
{
	H = 0;
	V = 0;
}

Bilddaten::Bilddaten(int neuH, int neuV)
{
	H = neuH;
	V = neuV;
}

This code still has a problem because C and Field, which uses H and V in the initialization, has already been initialized when the assignment in the constructor body happens. To fix this problem you need to initialize H and V in the constructor's initializer list.

1
2
3
4
5
6
7
8
9
10
11
Bilddaten::Bilddaten()
:	H(0),
	V(0)
{
}

Bilddaten::Bilddaten(int neuH, int neuV)
:	H(neuH),
	V(neuV)
{
}

This works because member variables are initialized in the order in which they are defined inside the class so C and Field will be initialized after H and V, but personally I find it bit confusing seeing initializations that depends on each other spread out like this so I would probably have moved all initializations to the constructor.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Bilddaten::Bilddaten()
:	H(0),
	V(0),
	C(H * V),
	Field(new int[C])
{
}

Bilddaten::Bilddaten(int neuH, int neuV)
:	H(neuH),
	V(neuV),
	C(H * V),
	Field(new int[C])
{
}

x, y and i doesn't look like they should be member variables, not sure about counter, so I left them out from the code above.

Now it is easier to see what is going on, at least for me. To get rid of the code repetition we can delegate the default constructor to the other constructor.

1
2
3
4
5
6
7
8
9
10
11
12
Bilddaten::Bilddaten()
:	Bilddaten(0, 0)
{
}

Bilddaten::Bilddaten(int neuH, int neuV)
:	H(neuH),
	V(neuV),
	C(H * V),
	Field(new int[C])
{
}
Last edited on
Thanks for your help. i really appreciate it
Topic archived. No new replies allowed.