Class problem?

Trying to make this run time array automatic, yet it's not really working within my constructor?

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
  #include <iostream>

using namespace std;
struct bob {
#pragma once
#ifndef bob_s
#define bob_s
private:
	int* x;
	int bytes;
	int go;
	bool A = false;
public:
	int& operator [] (int index) {
		return x[index];
		go = index;
		A = true;
	}
	bob();
	~bob();

};
#endif
bob::bob() {
	cout << "Constructing the array ";
	x = new int[30];
	if (A == true) {
		for (int i = 0; i < go; i++) {
			cout << x[i];
			bytes = bytes + sizeof(x[i]);
		}
		cout << endl;
		cout << "Your array is " << bytes << "Bytes\n";
	}
}
bob::~bob() {
	cout << "Destroying the array ";
	delete[] x;
}
int main() {
	bob k;
	char response;
	int a;
	cout << "enter in a value ";
	cin >> a;
	k[1] = a;
	cin >> response;
	return 0;
}
You are struggling with sequence of events. For example, on lines 14..18, the first thing you do is return from the function. Nothing else in the function will happen. (Crank up the warnings on your compiler and it will complain to you about that.)

- The constructor happens just once, when you create the object.
- Likewise, the constructor happens just once, when the object is destroyed.
- The operator[] will happen whenever you use it, which can only be after the constructor runs and before the destructor runs.

As a result, 'A' will have some random value when the constructor runs. The constructor's purpose is to give all the class variables a value before they are used.


It is not clear, but what exactly are you trying to accomplish? (What do you mean by an "automatic" run time array?)

Do you mean that you want the array to grow automatically when the user accesses an element?

For example, after construction, the array has zero elements.
User assigns a value to element at index 2.
Array now has 3 elements (indexed 0, 1, and 2).
?
What I was trying to accomplish by an automatic run time array was that after I called my operator and gave it a value it'd then do all of that inside the constructor. For instance, display all of the indexes and values and the size of the array. However now I know you can't do that since it only runs once. What I tried was this, however it doesn't really work...

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
#include <iostream>

using namespace std;
struct bob {
#pragma once
#ifndef bob_s
#define bob_s
private:
	int* x;
	int bytes;
	int go;
public:
	int& operator [] (int index) {
		go = index;
		return x[index];
	}
	bob();
	~bob();
	void give() {
		for (int i = 0; i < go; i++) {
			cout << x[i];
			bytes = bytes + sizeof(x[i]);
		}
		cout << endl;
		cout << "Your array is " << bytes << "Bytes\n";
	}

};
#endif
bob::bob() {
	cout << "Constructing the array ";
	x = new int[30];
}
bob::~bob() {
	cout << "Destroying the array ";
	delete[] x;
}
int main() {
	bob k;
	char response;
	int a;
	cout << "enter in a value ";
	cin >> a;
	k[1] = a;
	k.give();
	cin >> response;
	return 0;
}

Thanks for helping me!
Last edited on
What the heck is wrong with this?

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
#include <iostream>
#include <climits>

using namespace std;
struct bob {
#ifndef bob_s
#define bob_s
private:
	int* x;
	int bytes;
	int go;
public:
	int& operator [] (int index) {
		go = index;
		return x[index];
	}
	bob();
	~bob();
	void give() {
		for (int i = 0; i < go; i++) {
			cout << x[i];
			bytes = bytes + sizeof(x[i]);
		}
		cout << endl;
		cout << "Your array is " << bytes << "Bytes\n";
	}

};
#endif
bob::bob() {
	cout << "Constructing the array ";
	x = new int[1];
}
bob::~bob() {
	cout << "Destroying the array ";
	delete[] x;
}
int main() {
	bob k;
	char response;
	int a;
	cout << "enter in a value ";
	cin >> a;
	k[a] = a;
	k.give();
	cin >> response;
	return 0;
}

For some reason that works, however this doesn't?
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
#include <iostream>
#include <climits>

using namespace std;
struct bob {
#ifndef bob_s
#define bob_s
private:
	int* x;
	int bytes;
	int go;
public:
	int& operator [] (int index) {
		go = index;
		return x[index];
	}
	bob();
	~bob();
	void give() {
		for (int i = 0; i < go; i++) {
			cout << x[i];
			bytes = bytes + sizeof(x[i]);
		}
		cout << endl;
		cout << "Your array is " << bytes << "Bytes\n";
	}
	void setIndex(int a[], const int &nV, const int &JC) {
	    a[nV] = JC;
	}

};
#endif
bob::bob() {
	cout << "Constructing the array ";
	x = new int[100];
}
bob::~bob() {
	cout << "Destroying the array ";
	delete[] x;
}
int main() {
	bob k;
	char response;
	int a,b;
	int JK[5] = {1, 2, 3, 4, 5};
	cout << "enter in a value ";
	cin >> a;
	k[a] = a;
	k.give();
	int c;
	cin >> b >> c;
	k.setIndex(JK, b, c);
	for (int i = 0; i < b; i++) {
	    cout << JK[i];
	}
	    
	cin >> response;
	return 0;
}
lines 6,7,32: What the point of the include guard inside the struct? BTW, if bob_s is true, you have an incomplete struct definition (no }; ).

Line 27: Why are nV and JC reference arguments? You don't modify them.

Line 21: Only x[go] has a valid value in it (from line 48). Printing 0-go is going to print garbage elements.

Line 22: A multiplication statement would have been simpler:
1
2
 
  bytes = go * sizeof(x[i]);





Wow, I'm embarassed now haha. Well then, I thought that x[go] would set my array to the value length of that value, I now know i was wrong. I thought it was actually working when it wasn't xD, since all I was printing was garbage if i entered a value of which go was bigger than the one it was set to in the constructor. I changed it now though, I believe this is correct. Sorry for all the weird things/errors in the program. I thought include guards were necessary? Now that I think about those reference arguments I have no idea what I was thinking if I was thinking. I've been trying to learn/use classes and structs and so right now I'm noob at them. Man I completely misunderstood what I was trying to do with the int& operator. It was actually setting a certain value for an index in my array :P like you said. I was confused when you said that but I think I got it now.
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
#include <iostream>
#include <climits>

using namespace std;
struct bob {
#ifndef bob_s
#define bob_s
private:
	int* x;
	int bytes;
	int go;
public:
	int& operator [] (int index) {
		go = index;
		return x[index];
	}
	bob(int a);
	~bob();
	void give() {
		for (int i = 0; i < go; i++) {
			cout << x[i];
			bytes = bytes + sizeof(x[i]);
		}
		cout << endl;
		cout << "Your array is " << bytes << "Bytes\n";
	}

};
#endif
bob::bob(int a) {
	cout << "Constructing the array ";
	x = new int[a];
}
bob::~bob() {
	cout << "Destroying the array ";
	delete[] x;
}
int main() {
	char response;
	int a;
	cout << "Enter in a value ";
	cin >> a;
	bob k(a);
	k[a] = a;
	k.give();
	cin >> response;
	return 0;
}
Last edited on
I thought include guards were necessary?

They are. Nothing AbstractionAnon said suggested otherwise.

Do you understand how they work?
Not really I think it is something so that you don't introduce the same struct twice or something within a file. I thought I was using them correctly?
Whoa wait a second, this was only working on cpp.sh but not on c9 or visual studio..... What could be wrong I thought it was right?
For one thing on vs or c9 it gives me -842150451 or something and then when I try to exit the program it gives heap corruption detected error. CRT detected that the application wrote to memory after end of heap buffer in vs not c9. What have I done O.o
Last edited on
I think it is something so that you don't introduce the same struct twice or something within a file. I thought I was using them correctly?

Include guards are usually used to prevent the contents of a .h file from being included multiple times. You have used them in a .cpp file. You would not want to include this .cpp in other programs, so include guards within a .cpp file are superfluous.

And as I pointed out earlier, yours are wrong. if bob_s were already defined, your include would effectively look like this:
1
2
3
struct bob {
//  #ifndef through #endif skipped
//  note: no closure on the structure body 


Line 43: Allocates an array of size a. Let's assume a is 10.
Line 44: Assigns a to k[a]. This is an out of bounds reference. Rewriting line 44:
k[10] = 10;
You allocated 10 elements [0-9]. Element [10] is out of bounds.

Line 21: You're still printing uninitialized elements.
Last edited on
I still don't know what this code is supposed to do.
All it's supposed to do is make me a dynamic array to whatever I want :p. Then print out all the values and the amount of memory in bytes and then delete it when i'm done. I know it's something simple and doesn't need a class. I just made it in a class so that I could use it anywhere without having to copy and paste and be able to use it anywhere. This is the final version as it works with both VS and c9...

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
#include <iostream>
#include <climits>

using namespace std;
struct bob {
private:
	int* x;
	int bytes;
	int go;
	int counter;
public:
	int& operator [] (int index) {
		return x[index];
	}
	bob(int a);
	~bob();
	void give() {
		for (int i = 0; i < go; i++) {
			cout << x[i];
		}
		cout << endl;
		cout << "Your array is " << bytes << "Bytes\n";
	}

};
bob::bob(int a) {
	cout << "Constructing the array ";
	x = new int[a];
	go = a;
	bytes = a * sizeof(x[0]);
	for (int i = 0; i < a; i++) {
		x[i] = 0;
	}
}
bob::~bob() {
	cout << "Destroying the array ";
	delete[] x;
}
int main() {
	char response;
	int a, b;
	cout << "Enter in a value ";
	cin >> a;
	b = a - 1;
	bob k(a);
	k.give();
	cin >> response;
	return 0;
}
Last edited on
? You can get that same information using a std::vector.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>

int main()
{
  std::cout << "n? ";
  unsigned n;
  std::cin >> n;
  if (!std::cin) return 1;

  std::cout << "Creating the array.\n";
  std::vector <int> xs( n );

  std::cout << "Number of elements in the array = " << xs.size() << ".\n";
  std::cout << "Number of bytes used by the array = " << (xs.size() * sizeof(int)) << ".\n";

  std::cout << "Destroying the array.\n";
  std::vector <int> ().swap( xs );
}
Last edited on
Lol Duoas it just that I'm very inexperienced with vectors. Although vectors are better than arrays I just made this one with an array and a class :P.
Ah. It's good to mess around to figure out how stuff works.
Topic archived. No new replies allowed.