vector of objects error

this code is giving me an error in the neurons[0].GetArrInput(arr);
function

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
// AI.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <vector>

const int num_inputs = 20;

int inputs[num_inputs];

using namespace std;

class neuron {
	neuron *inputs[100];
public:
	int val[100];
public:
	void GetArrInput(int arr[]) {
		for (int i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) {
				val[i] = arr[i];
		}
	}
	void GetInputs() {
		int j = 0;
		for (int i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) {
			while (j<10) {
				val[i] = inputs[i]->output[j];
				j++;
			}
		}
	}
	void Process() {

	}
	int output[10];
};

class brain {
public:
	vector<neuron> neurons;
	void CreateBrain (int arr[]){
		for (int i = 0; i > sizeof(inputs[0]) / sizeof(inputs); i++) {
		neurons.push_back(neuron());
		}
		neurons[0].GetArrInput(arr);
	}
	void ShowNeuron() {
		for (int i = 0; i<101; i++) {
			cout << neurons[0].val[i] << endl;
		}
	}
};

int main()
{
	int arr[100] = {
		1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,
	};
	brain tom;
	tom.CreateBrain(arr);
	tom.ShowNeuron();
	system("pause");
    return 0;
}
Last edited on

 In member function 'void neuron::GetArrInput(int*)':
20:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 In member function 'void neuron::GetInputs()':
26:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 In member function 'void brain::CreateBrain(int*)':
43:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 In member function 'void brain::ShowNeuron()':
50:28: warning: iteration 100u invokes undefined behavior [-Waggressive-loop-optimizations]
49:3: note: containing loop

Your array has 100 elements. That means the indices go from 0 to 99, inclusive. Change the 101 in your for loop to a 100.

Also, a suggestion:
1
2
3
int arr[100] = {
		1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,
	};

Oh my. There's this convenient operator called the modulo operator, %. You can think of it as giving the remainder of the number.

1
2
3
4
5
6
7
8
int arr[100];
for (int i = 0; i < 100; i++)
{
    if (i % 10 == 0)
        arr[i] = 10;
    else
        arr[i] = (i + 1) % 10;
}
Last edited on
thx that array isnt actually going to be used its just for testing purposes
its still giving me the error vector subscript out of range at line 47
Here:
1
2
3
4
5
    void CreateBrain (int arr[]){
		for (int i = 0; i > sizeof(inputs[0]) / sizeof(inputs); i++) {
		neurons.push_back(neuron());
		}
		neurons[0].GetArrInput(arr);


i is initialized with value 0. 0 will never be greater than sizeof(inputs[0])/sizeof(inputs). Therefore, nothing will be added to neurons and neurons[0] will not exist on the last line of the code shown above.
yes but it should increment the value by one every time it runs through therefore it should still create the neuron in that section and eventually reach 100
Last edited on
Oh, good catch cire, I didn't see that.
yes but it should increment the value by one every time it runs through

Which value are you referring? i is being set to 0 every time, and sizeof operator will never, ever return a negative number.

Both sizeof(inputs[0]) and sizeof(inputs) are constant numbers. Your loop never runs.
Unhandled exception at 0x7720CBB2 in AI.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x003FED38. occurred on line 45
Last edited on
what im trying to do here is create a "neuron" (my class object) inside of a vector of said class and call it from a function of the brain class but it doesn't seem to be creating the object in the vector so i can access it
ive also tryed 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// AI.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <vector>

const int num_inputs = 20;

int inputs[num_inputs];

using namespace std;

class neuron {
	neuron *inputs[100];
public:
	int val[100];
public:
	void GetArrInput(int arr[]) {
		for (int i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) {
				val[i] = arr[i];
		}
	}
	void GetInputs() {
		int j = 0;
		for (int i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) {
			while (j<10) {
				val[i] = inputs[i]->output[j];
				j++;
			}
		}
	}
	void Process() {

	}
	int output[10];
};

class brain {
public:
	vector<neuron*> neurons;
	void CreateBrain(int arr[]) {
		for (int i = 1; i > sizeof(inputs[0]) / sizeof(inputs); i++) {
			neurons.push_back(new neuron);
		}
		neurons[0]->GetArrInput(arr);
	}
	void ShowNeuron() {
		for (int i = 0; i<10; i++) {
			cout << neurons[0]->val[i] << endl;
		}
	}
};

int main()
{
	int arr[100] = {
		1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,
	};
	brain tom;
	tom.CreateBrain(arr);
	tom.ShowNeuron();
	system("pause");
    return 0;
}
You have the same mistake you made previously. 1 is not greater than sizeof(inputs[0])/sizeof(inputs) and the body of the loop is never entered, so there are no elements in neurons on line 47.

Think about things rather than changing them and hoping it helps.
i get what your saying it theirs a greater than sign their instead of a less than sign i didn't see that before and i didnt realize that you pointed it out very well. i also changed the loop because keeps increasing the size of the vector so it will never end thanks i didnt under stand what you were saying at first
Last edited on
> i get what your saying it theirs a greater than sign their instead of a less than sign
nice edit.

> i didn't see that before and you didn't point it out very well.
yes, our fault.

> i also changed the loop because keeps increasing the size of the vector so it will never end
not even close.


¿care to show your updated «fixed» code?
closed account (E0p9LyTq)
sizeof(inputs) / sizeof(inputs[0] isn't doing what I suspect you think it does.

All that will do is give you the number of elements in your array, which is oversized to 100. Not the actual number of valid elements.

Why use a C style array when a vector automatically keeps track of the number of elements assigned to it.
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
// AI.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <vector>

const int num_inputs = 20;

int inputs[num_inputs];

using namespace std;

class neuron {
	neuron *inputs[100];
public:
	int val[100];
public:
	void GetArrInput(int arr[]) {
		for (int i = 0; i < sizeof(arr[0]) / sizeof(arr); i++) {
				val[i] = arr[i];
		}
	}
	void GetInputs() {
		int j = 0;
		for (int i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) {
			while (j<10) {
				val[i] = inputs[i]->output[j];
				j++;
			}
		}
	}
	void Process() {

	}
	int output[10];
};

class brain {
public:
	vector<neuron> neurons;
	void CreateBrain(int arr[]) {
		int aray[sizeof(arr[0]) / sizeof(arr)];
		for (int i = 0; i < sizeof(arr[0]) / sizeof(arr); i++) {
			aray[i] = arr[i];
		}
		for (int i = 0; i < sizeof(inputs[0]) / sizeof(inputs); i++) {
			neurons.push_back(neuron());
		}
		neurons[0].GetArrInput(aray);
	}
	void ShowNeuron() {
		for (int i = 0; i<10; i++) {
			cout << neurons[0].val[i] << endl;
		}
	}
};

int main()
{
	int arr[100] = {
		1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,
	};
	brain tom;
	tom.CreateBrain(arr);
	tom.ShowNeuron();
	system("pause");
    return 0;
}

here is the current code and this is the only way i know how to get the number of element im not to great with vectors
read the warnings
|| foo.cpp: In member function ‘void neuron::GetArrInput(int*)’:
foo.cpp|21 col 50| warning: ‘sizeof’ on array function parameter ‘arr’ will return size of ‘int*’ [-Wsizeof-array-argument]
||    for (int i = 0; i < sizeof(arr[0]) / sizeof(arr); i++) {
|| foo.cpp: In member function ‘void brain::CreateBrain(int*)’:
foo.cpp|44 col 39| warning: ‘sizeof’ on array function parameter ‘arr’ will return size of ‘int*’ [-Wsizeof-array-argument]
||    int aray[sizeof(arr[0]) / sizeof(arr)];
||                                        ^
pass the size of the array as an extra parameter to every function
i got neither of those warnings I'm going try and fix all of this myself because i am not great at working with writing code in c++ I've only been working with it for a month or so and a believe my ignorance is annoying certain people. thank you for your suggestions and help i appreciate it.
i got neither of those warnings
If you're compiling with GCC or Clang (or another interface-compatible compiler), pass the flags -Wall -Wextra -pedantic-errors to the compiler on the command line.
If you're compiling with Microsoft Visual C++, see this helpful post by @JLBorges
http://www.cplusplus.com/forum/beginner/211517/#msg990484
Last edited on
Topic archived. No new replies allowed.