problems with objects in vector

Apr 17, 2010 at 9:01am
I'm quite new to c++, and this is the first time i've used vectors so please bear with me;)

I have a vector that stores objects of class Note. I can push_back and retrieve objects just fine except for one case, where I'm trying to get not the current object but the previous object in a for-loop. Here is the complete code, with the section I believe to be the culprit marked with comments: http://pastebin.org/154570

Thanks,
Fafner
Apr 17, 2010 at 12:52pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int charFind(char array[], char val) {
        int length = (sizeof(array)/sizeof(char)); //Error -cannot calculate the size of an array like this  when it is passed as a function parameter
        int cnt;
        for (int i = 0; i < length; i++) {
                char a = array[i];

                if (a == val) {

                        break;
                }

                else {
                        cnt++;
                }
        }

        return cnt;
}
Apr 17, 2010 at 1:05pm
1
2
3
4
5
6
int charFind(char array[], char val, int length) {
        for (int i = 0; i < length; i++)
                if (array[i] == val)
                        return i;
        return -1;
}
Last edited on Apr 17, 2010 at 1:08pm
Apr 17, 2010 at 2:03pm
Ok, I changed it like you said, but the same lines still crashes at runtime. The code is now:

pIndex = charFind(majorScale, sSize, melody[i-1].getPitch());
dIndex = intFind(durations, dSize, melody[i-1].getDuration());

and the mentioned functions is:

int charFind(char array[], int length, char val) {
int cnt;
for (int i = 0; i < length; i++) {
char a = array[i];
if (a == val) {
break;
}
else {
cnt++;
}
}
return cnt;
}

The same goes for the intFind(). I wasnt aware that you couldn't calculate the size that way if it is passed as an argument, so thanks for that;) But still the program crashes...
Apr 17, 2010 at 2:11pm
You are trying to get the index of an uninitialised vector.
Apr 17, 2010 at 2:30pm
The vector is declared a couple of lines above, like this:

vector<Note> melody;

Is there anything more I need to do to initialize it?
Apr 17, 2010 at 3:22pm
Your current code looks like 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
       scanf("%d", &length);

        vector<Note> melody; // Create an empty vector
     

        for (int i = 0; i < length; i++) {

                Note n;

                if (i == 0) {

                        n.setPitch(majorScale[rand()%sSize]);

                        n.setDuration(durations[rand()%dSize]);

                }

                else if (i > 0) {

                        int pIndex;

                        int dIndex;
                     

                        //Appears to be problem area
                        //Severe carsh here!! 
                        //melody vector is still empty (it has no Note objects in it yet)
                        //and we are trying to  access a Note object at  melody[i-1]
                        //Fatal Error
                        pIndex = charFind(majorScale, melody[i-1].getPitch());

                        dIndex = intFind(durations, melody[i-1].getDuration());

                        //End of problem area

                       

                        //Pitch

                        if (pIndex == 0) 



can I assume that what you rare trying t to do is to check if a partcicular Note already exist in
the vector or something similar??
Apr 17, 2010 at 5:01pm
It would perhaps appear so from the code that I posted, but the n-object is added to the vector at the end of the for-loop, just after the else if-statement. So the note is/should be in the vector. Here is the complete code: http://pastebin.org/155016

Apr 17, 2010 at 8:19pm
In the intFind and charFind functions - the variable cnt is not correctly used.
It is not initialised - so cnt++ is effectively random - so you are returning a garbage value.

Apr 18, 2010 at 9:47am
YES, THANK YOU!! That was it, thank you so much;)
Topic archived. No new replies allowed.