considering this code i put a bp at the end of roll(int n) and i had data in values array
and i put another one at the end of print and there was no data in the array.Why do I get this error: CXX0069: Error: variable needs stack frame?
die.h
#ifndef DIE_H
#define DIE_H
#include<iostream>
#include<time.h>
using namespace std;
class Die
{
private:
int number;
int values[6][2];
void roll();
public:
Die();
void Roll(int n);
int getNumber()const{return number;}
void printLastValue();
void printApearences();
~Die(){}
};
#endif
die.cpp
#include"die.h"
#include<iostream>
#include<time.h>
using namespace std;
Die::Die()
{
srand(static_cast<int>(time(NULL)));
number=0;
for(int j=0;j<6;j++)
{
values[j][0]=j+1;
values[j][1]=0;
}
}
void Die::roll()
{
number=1+rand()%6;
}
void Die::printLastValue()
{
cout<<number<<endl;
}
void Die::Roll(int n)
{
for(int j=0;j<6;j++)
{
values[j][0]=j+1;
values[j][1]=0;
}
for(int i=0;i<n;i++)
{
roll();
(values[number-1][1])++;
}
}
void Die::printApearences()
{
for(int i=0;i<6;i++)
{
cout<<values[i][0]<<" : "<<cout<<values[i][1]<<endl;
}
}
main.cpp
#include"die.h"
#include<iostream>
using namespace std;
int main()
{
Die d;
d.Roll(5);
d.printApearences();
}
What exactly is this:
Specifically, why are you trying to extract
couttocout? Copy/paste can be a ruthless wench. Pretty sure you want:Next, your header declarations are quite-convoluted.
using namespace stdin any of your header files. For information on why, refer this question and its various discussions. If your fingers tire of typingstd::there are some alternatives, but in-general slurping an entire namespace (especially one as large asstd) can cause unintended consequences. The linked question is worth a review.#include‘s into a header unless the content therein is dependent on it (Die.h needs nothing you’re#include‘ing, for example).<cstdio>,<cstdlib>,<ctime>, etc.Applying the above your code becomes:
Die.h
Die.cpp (top of file, code eliminated for brevity)
main.cpp (top of file, code eliminated for brevity)
The last one traditionally contains
<iostream>as well, but you don’t actually need it in your code as it is written.You’re static cast to
intfor sendingtime(NULL)as the random seed is not correct. Thesrand(unsigned int seed);API takes anunsigned intas the seed, not anint. Change your seeding to beI’d start with those, specifically the first two suggestions.