PLease help me with this pointer

I have a runtime error that i cannot figure out. The problme is in line (77 ).

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
#include <vector>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;

class RandomWheel
{
     private:
        int currentDice;
        int numberInDice;
        int numberOfDice;
        //const int WHEELSIZE;
        vector < int > Dicevalues ;
        vector <string> labels ;
    public:
        ///Constructor
        RandomWheel();
        ///Mutators
        void Spin();
        ///Accessors
        int GetCurrentValue() const;
        string GetCurrentLabel() const;
};

RandomWheel::RandomWheel()
{
    srand(time(0));
    numberInDice = 0;// dEFAULT
    ///numberOfDice = length;
    currentDice= 0;
    const int WHEELSIZE = 6;
    vector <int> Dicevalues(WHEELSIZE);
    vector <string> labels (WHEELSIZE);
    labels[0] = "Butterfly";
    labels[1] = "Cockroach";
    labels[2] = "Cinderella";
    labels[3] = "Pikachu";
    labels[4] = "Shrek";
    labels[5] = "Scareface";

    for (int i = 0; i< 6; i ++ )
    {
        Dicevalues[i] = i + 1;
    }

}

void RandomWheel::Spin()
{

    numberInDice = rand()% 5;
}

string RandomWheel:: GetCurrentLabel() const
{
    return (labels[numberInDice]) ;
}

int RandomWheel:: GetCurrentValue() const
{
    return (Dicevalues[numberInDice]);
}

int PlayOneRound(int VectorLength)
{
    vector <RandomWheel* > dicelist (VectorLength) ;
    for (int i = 0; i <VectorLength ; i++)
    {
       dicelist[i] = new RandomWheel;
    }
 for (int i = 0; i < VectorLength; i++)
    {
        dicelist[i]->Spin();
        //****This line is the problem!!***
        //cout <<"Dice"<<i<<" is a "<<dicelist[i]->GetCurrentLabel()<<" : ";
    }
}


THe problem has something to do with accessing a piece of memory i do not own, or have not initialized, something like that, but i have checked throughout and cannot find the fix.
Help would be greatly appreciated
Thankyou.
Last edited on
Can I ask why you're using pointers for this? Can you not simply use a vector<RandomWheel> itself?
i know, the teacher said so
Last edited on
What are you compiling this with? It's a remarkably lax compiler, given that your function PlayOneRound claims to return an int but in fact returns nothing. Does it not even warn you about this?

That aside, run this and follow the clues :)


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
#include <vector>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <iomanip>
using namespace std;

class RandomWheel
{
     private:
        int currentDice;
        int numberInDice;
        int numberOfDice;
        //const int WHEELSIZE;
        vector < int > Dicevalues ;
        vector <string> labels ;
    public:
        ///Constructor
        RandomWheel();
        ///Mutators
        void Spin();
        ///Accessors
        int GetCurrentValue() const;
        string GetCurrentLabel() const;
};

RandomWheel::RandomWheel()
{
    srand(time(0));
    numberInDice = 0;// dEFAULT
    ///numberOfDice = length;
    currentDice= 0;
    const int WHEELSIZE = 6;
    vector <int> Dicevalues(WHEELSIZE);
    vector <string> labels (WHEELSIZE);
    labels[0] = "Butterfly";
    labels[1] = "Cockroach";
    labels[2] = "Cinderella";
    labels[3] = "Pikachu";
    labels[4] = "Shrek";
    labels[5] = "Scareface";

    for (int i = 0; i< 6; i ++ )
    {
        Dicevalues[i] = i + 1;
    }

}

void RandomWheel::Spin()
{

    numberInDice = rand()% 5;
}

string RandomWheel:: GetCurrentLabel() const
{
  cout << "What is the current size of labels? " << labels.size() << endl; 
    return (labels[numberInDice]) ;
}

int RandomWheel:: GetCurrentValue() const
{
    return (Dicevalues[numberInDice]);
}

int PlayOneRound(int VectorLength)
{
    vector <RandomWheel* > dicelist (VectorLength) ;
    for (int i = 0; i <VectorLength ; i++)
    {
       dicelist[i] = new RandomWheel;
    }
 for (int i = 0; i < VectorLength; i++)
    {
        dicelist[i]->Spin();
        //****This line is the problem!!***
        cout <<"Dice"<<i<<" is a "<<dicelist[i]->GetCurrentLabel()<<" : ";
    }
}

int main()
{
  PlayOneRound(50);
}
  

Last edited on
thanks, but why is the labels vector size 0, i accessed elements 0 - 5 and gave a label to each element.
Or how do you do what i want to do correctly
Take a look at this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
RandomWheel::RandomWheel()
{
    srand(time(0));
    numberInDice = 0;// dEFAULT
    ///numberOfDice = length;
    currentDice= 0;
    const int WHEELSIZE = 6;
    vector <int> Dicevalues(WHEELSIZE); 
    vector <string> labels (WHEELSIZE); // Here you create a local variable named labels.
    labels[0] = "Butterfly"; // And here you do things with that local variable...
    labels[1] = "Cockroach";
    labels[2] = "Cinderella";
    labels[3] = "Pikachu";
    labels[4] = "Shrek";
    labels[5] = "Scareface";

    for (int i = 0; i< 6; i ++ )
    {
        Dicevalues[i] = i + 1;
    }
  // And here the function ends. What happens to local variables when a function ends?
}


If you're still stuck, read this: http://en.wikipedia.org/wiki/Variable_shadowing
Last edited on
THANKYOU, you just saved me 2 hours of debugging
Here is how I found your error. It took about sixty seconds once I'd written the main function as above.


j@j-desktop:~/badCode$ gdb ./a.out 
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/j/badCode/a.out...done.
(gdb) run
Starting program: /home/j/badCode/a.out 
Dice0 is a What is the current size of labels? 0

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b72018 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () from /usr/lib/libstdc++.so.6
(gdb) bt
#0  0x00007ffff7b72018 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) ()
   from /usr/lib/libstdc++.so.6
#1  0x0000000000401b13 in RandomWheel::GetCurrentLabel (this=0x608030)
    at 097.cpp:60
#2  0x0000000000401d61 in PlayOneRound (VectorLength=1) at 097.cpp:78
#3  0x0000000000401e4b in main () at 097.cpp:85
(gdb) list
76	        dicelist[i]->Spin();
77	        //****This line is the problem!!***
78	        cout <<"Dice"<<i<<" is a "<<dicelist[i]->GetCurrentLabel()<<" : ";
79	    }
80	}
81	
82	
83	int main()
84	{
85	  PlayOneRound(1);
(gdb) up
#1  0x0000000000401b13 in RandomWheel::GetCurrentLabel (this=0x608030)
    at 097.cpp:60
60	    return (labels[numberInDice]) ;
(gdb) up
#2  0x0000000000401d61 in PlayOneRound (VectorLength=1) at 097.cpp:78
78	        cout <<"Dice"<<i<<" is a "<<dicelist[i]->GetCurrentLabel()<<" : ";
(gdb) down
#1  0x0000000000401b13 in RandomWheel::GetCurrentLabel (this=0x608030)
    at 097.cpp:60
60	    return (labels[numberInDice]) ;
(gdb) list
55	}
56	
57	string RandomWheel:: GetCurrentLabel() const
58	{
59	  cout << "What is the current size of labels? " << labels.size() << endl; 
60	    return (labels[numberInDice]) ;
61	}
62	
63	int RandomWheel:: GetCurrentValue() const
64	{
(gdb) print labels
$3 = {<std::_Vector_base<std::string, std::allocator<std::string> >> = {
    _M_impl = {<std::allocator> = {<__gnu_cxx::new_allocator<std::string>> = {<No data fields>}, <No data fields>}, _M_start = 0x0, _M_finish = 0x0, 
      _M_end_of_storage = 0x0}}, <No data fields>}
(gdb) 


Every line beginning (gdb) is something I typed.
Yikes, look at that! labels has <No data fields>!

Learning to use a debugger will save you so much time.
Last edited on
So now i got rid of the two new local vectors i created in the constructor, and still have the same problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
RandomWheel::RandomWheel()
{
    srand(time(0));
    numberInDice = 0;// dEFAULT
    ///numberOfDice = length;
    currentDice= 0;
    //const int WHEELSIZE = 6;
    string names[6] = {"Butterfly, Cockroach, Cinderella, Pikachu, Shrek, Scareface"};
    int points[6] = {1,2,3,4,5,6};
   
    labels[0] = "Butterfly";
    labels[1] = "Cockroach";
    labels[2] = "Cinderella";
    labels[3] = "Pikachu";
    labels[4] = "Shrek";
    labels[5] = "Scareface";

    for (int i = 0; i< 6; i++)
    {
        Dicevalues[i] = i +1;
    }
}
string names[6] = {"Butterfly, Cockroach, Cinderella, Pikachu, Shrek, Scareface"};
What is this for? You create it and then never use it.

int points[6] = {1,2,3,4,5,6};
Likewise.


labels[0] = "Butterfly";
At this point, what size is labels? Size zero. So what happens when you try to change the value of labels[0], which does not exist? Bad things.
Last edited on
i was doing stuff with those things,

why do labels[0 - 5] dont exist?? Do you mean the labels are not inside the label vector even though i went one by one = "a string", why size is 0.
Also i am not trying to change what i am going to put inside each element of the labels vector.

SO how do you put those labels in the label vector correctly please???!

why do labels[0 - 5] dont exist??

Why would they exist? Did you create them? No.

1
2
vector < int > Dicevalues ;
vector <string> labels ;

This creates two vectors, of size zero. If you want to start adding elements to them, you can use push_back, which will add a new element to the end, or you can resize them yourself to some suitable size:

1
2
Dicevalues.resize(6);
labels.resize(6);
Last edited on
1
2
3
4
5
6
7
8
string names[6] = {"Butterfly, Cockroach, Cinderella, Pikachu, Shrek, Scareface"};
int points[6] = {1,2,3,4,5,6};

    for (int i =0; i< 6; i++)
    {
        labels.push_back(names[i]);
        Dicevalues.push_back(points[i]);
    }


why does this does not work either?
Read about push_back()

http://www.cplusplus.com/reference/stl/vector/push_back/

Another person asked about this assignment recently and had code for a fruit wheel that they wanted to adapt for dice game.

Did your teacher require you to change the methods of the RandomWheel class because in my practice solution I left the class unchanged.
"Butterfly, Cockroach, Cinderella, Pikachu, Shrek, Scareface"};
That's one string.

Did you mean this?
"Butterfly", "Cockroach", "Cinderella", "Pikachu", "Shrek", "Scareface"};
Topic archived. No new replies allowed.