Badlittlepiggy Badlittlepiggy - 1 year ago 72
C++ Question

C++ class relies on local declorations

I am writing a Class that contains functions that will create a multi dimensional array bool array, store bool values in multi dimensional as well as print the multi dimensional array.

So far my class works fine for now. however I would like my class functions to not have to create a local multi dimensional array for every time I want a class function to complete a task. It would be ideal if I could create it as a member data but I am not sure that is possible or at least I have done it wrong so many times it made me believe it is impossible.

Side note I am still new with c++ so please do not submit an answer that includes pointers because I have no idea how they work and it will only confuse me more at the moment.

Here is my code and thank you for your time and input it is very much appreciated!

//class declorations.
// .h file
#ifndef STACK_H
#define STACK_H
using namespace std;

class stackClass{
const static int index_one = 10;
const static int index_two = 10;
//const static bool boolray[index_one][index_two];
void set(const int iacross, const int ivert);
void print();

int across;
int vert;
const static bool the_array[index_one][index_two];


// class definitions
// .cpp file

#include "stack.h"
using namespace std;

stackClass :: stackClass(){
bool the_array[index_one][index_two];
for(int across = 0; across < index_one; across++)
for(int vert = 0; vert < index_two; vert++)
the_array[across][vert] = false;

void stackClass :: print(){
bool the_array[index_two][index_one];
for (int across = 0; across < index_one; across++){
for (int vert = 0; vert < index_two; vert++){
if(the_array[across][vert] == true){
cout << "*";
cout << " ";

cout << endl;

void stackClass :: set(int iacross, int ivert){
// clinet program for testing
// .cpp file
#include "stack.h"
#include <cstdlib>
using namespace std;

int main(){

stackClass obj1;
for (int count = 0; count < 5; count++) {
obj1.set(rand()%20, rand()%20);

return 0;

Answer Source

There are a few issues with your code. First of all, your set function is not doing anything. You are assigning values to the parameters of the function. Did you mean to do across=iacross; instead (and same for ivert)?

Next, your constructor doesn't do anything useful either. It creates a local array and initializes it, but as soon as that function is over your local array will be gone.

Then your print function creates a new local array, doesn't initialize it, and then reads values from it. So it's just printing junk from the memory.

If I understand correctly, this is what you want to do. You have a private member the_array and you want to use it. First, it shouldn't be const, and probably not static (not sure why you made it static but it's up to you). Then in your constructor, get rid of the line

bool the_array[index_one][index_two];

That way the rest of the code will initialize the values of your member the_array. Also, in your print function, get rid of the same line, so that then you can read from the member array. Finally, in your set function, my best guess is this is what you want to do:

the_array[iacross][ivert] = true;

and nothing else.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download