0

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();
}
laura
  • 2,085
  • 13
  • 36
  • 68

3 Answers3

2

What exactly is this:

cout<<values[i][0]<<" : "<<cout<<values[i][1]<<endl;

Specifically, why are you trying to extract cout to cout? Copy/paste can be a ruthless wench. Pretty sure you want:

cout << values[i][0] <<" : "<< values[i][1] << endl;

Next, your header declarations are quite-convoluted.

  • Do NOT place using namespace std in any of your header files. For information on why, refer this question and its various discussions. If your fingers tire of typing std:: there are some alternatives, but in-general slurping an entire namespace (especially one as large as std) can cause unintended consequences. The linked question is worth a review.
  • Do not bring #include's into a header unless the content therein is dependent on it (Die.h needs nothing you're #include'ing, for example).
  • List your headers ahead of system headers, including in your headers, to ensure you do not code an implicit include that your header by-itself doesn't fulfill.
  • Include standard library C++ headers if you're compiling in C++ (use <cstdio>, <cstdlib>, <ctime>, etc.

Applying the above your code becomes:

Die.h

#ifndef DIE_H
#define DIE_H

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 (top of file, code eliminated for brevity)

#include "die.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

main.cpp (top of file, code eliminated for brevity)

#include "die.h"

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 int for sending time(NULL) as the random seed is not correct. The srand(unsigned int seed); API takes an unsigned int as the seed, not an int. Change your seeding to be

srand(static_cast<unsigned int>(time(NULL)));

I'd start with those, specifically the first two suggestions.

Community
  • 1
  • 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • "List system headers first, then yours." - that prevents compiler errors if one of those system headers is needed by your header but not included therefrom, so I recommend your own headers then system headers (at least for the library or test translation unit most closely related to that header). – Tony Delroy May 20 '13 at 07:56
  • 2
    @TonyD You're absolutely right, and I've since reversed that position (and shall update his answer accordingly). – WhozCraig May 20 '13 at 08:08
  • You left using namespace std in what the header should be. – LogicMagic Dec 22 '16 at 15:13
  • @LogicMagic I did? Where, in `Die.h` does this appear ? – WhozCraig Dec 22 '16 at 18:52
1

It doesn't recognize rand and srand. Add #include <stdlib.h> to your die.cpp file.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
1

The debugger will display this error when the execution is at a point where the variable in question is out of scope, and therefore cannot be evaluated. Step into your code until you reach the scope of the variable, and the debugger will show you its value.

Fred
  • 4,894
  • 1
  • 31
  • 48