Home > Net >  How to set pointer class to int value to pass into a function? How to set member of class to the fir
How to set pointer class to int value to pass into a function? How to set member of class to the fir

Time:02-05

I created 2 complex types BitsByte variable pointers and initially set them to nullptr. One will hold the uppers Byte of the word and the other will hold the lower Byte of the Word.

// File: BitsWord.h
class BitsWord
{
private:
int value;
int bits[16] = {};

    BitsByte* mLower{nullptr};
    BitsByte* mUpper{nullptr}; ...

I have a setValue function to set mUpper and mLower to the first two bytes of value that will convert the decimal number into the binary equivalent in an array named bits.

// BitsWord.cpp
void BitsWord::setValue(int value)
{ 
for(int i = 0; i < 16; i  )
    { bits[i] = static_cast<bool>(value & (1 << i)); }
reverse(begin(bits), end(bits));
}

I want to call the setValue function on mLower passing value. Debugger shows Thread 1: EXC_BAD_ACCESS (code=1, address=0x0) on Xcode when I'm attempting to set mLower to the first 8-bits of value.

*mLower = value;     setValue(value);

CodePudding user response:

You need to allocate memory to mLower before this line :

*mLower = value;

You should first create an instance of BitsByte, then assign the pointer mLower to point to that instance before calling setValue() function.

CodePudding user response:

This is actually a homework assignment that you might want to talk to the instructor about. I understand he is starting to use software similarity tools to detect plagiarism.

  • Related