laura
laura

Reputation: 2135

Error: variable needs stack frame

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();
}

Upvotes: 0

Views: 3244

Answers (3)

WhozCraig
WhozCraig

Reputation: 66194

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.

Upvotes: 2

Fred
Fred

Reputation: 4954

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.

Upvotes: 1

SomeWittyUsername
SomeWittyUsername

Reputation: 18348

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

Upvotes: 1

Related Questions