2

"ClassA.h"

class ClassA {
public:
classArray[10][5];

void loadArray(){
//loads ints into array
}
};

"ClassB.h"

class classB {
void outputArray(){
ClassA classA;
cout << classA.classArray[1][1];
}

};

Whenever I print the array[1][1], it outputs 0 to console. Despite having loaded 7020 into it. In the main both functions are called.

1
  • 1
    You need to call loadArray like classA.loadArray() before printing out. Commented Oct 20, 2018 at 11:19

2 Answers 2

1

When you do this:

ClassA classA;
cout << classA.classArray[1][1];

you invoke Undefined Behavior (UB), since the array of classA is used uninitialized. First, load the ints to it, and the print it, so you probably need to do this instead:

ClassA classA;                       // create an object
classA.loadArray();                  // fill the array of the object
cout << classA.classArray[1][1];     // print a specific element
Sign up to request clarification or add additional context in comments.

Comments

1

You have a newline initialized ClassA instance, and haven't called loadArray on it. Either call it explicitly:

ClassA classA;
classA.loadArray(); // Here
cout << classA.classArray[1][1];

Or, if possible, consider moving this logic to ClassA's constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.