0

The issue that I am currently experiencing is as follows:

I am attempting to construct a vector in my main.cpp file that contains only SingleBox objects (SingleBox is a class that I have defined), however, when I attempt to populate or resize that vector, the vector never contains any SingleBox objects and the code below the attempt to populate or resize the vector never executes in my main.cpp file.

Thank you in advance to anyone offering assistance in resolving this issue.

File main.cpp

#include <iostream>
#include "SingleBox.h"
#include "NineBox.h"

int main( int argc, const char * argv[] )
{
    std::vector< SingleBox > vec;
    std::cout << "Below is the attempt to populate: \n";
    vec.push_back( SingleBox( 0, 0 ) );
    std::cout << "Nothing from this point onward executes...\n";
    std::cout << "The size: " << vec.size() << std::endl;

    return 0;
}

File SingleBox.h

#ifndef __Sudoku_Solver__SingleBox__
#define __Sudoku_Solver__SingleBox__

#include <iostream>
#include <vector>

// SingleBox class definition
class SingleBox
{
public:
    SingleBox( int nineBoxNum, int boxValue ); // constructor

    void updatePoss( int number );             // function to remove disproven possibilities

    // get functions
    bool isComplete(  );                       // function to get the completion indicator
    int getBoxValue(  );                       // function to get the boxValue

    // debugging functions
    void debugPrinter(   );

private:
    bool completionStatus;                     // if the value of the SingleBox is non-zero
    int nineBoxNumber;                         // the NineBox to which the SingleBox belongs
    int boxValue;                              // the value of the SingleBox
    std::vector< int > possibilities;          // possible values for the SingleBox

    // set functions
    void setNineBoxNum( int nineBoxNum );      // function to set the NineBox number
    void setBoxValue( int boxVal );            // function to set the boxValue
    void setIndicator( bool isComplete );      // function to set the completion indicator
};

#endif /* defined(__Sudoku_Solver__SingleBox__) */

File SingleBox.cpp

#include "SingleBox.h"

// SingleBox constructor initializes each data member
SingleBox::SingleBox( int nineBoxNum, int boxVal )
{
    setNineBoxNum( nineBoxNum );
    setBoxValue( boxVal );
    setIndicator( false );
    possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9};
} // end SingleBox constructor

// set new NineBox number
void SingleBox::setNineBoxNum( int nineBoxNum )
{
    nineBoxNumber = nineBoxNum;
} // end function setNineBoxNum

// set new BoxValue number
void SingleBox::setBoxValue( int boxVal )
{
    boxValue = boxVal;
} // end function setBoxValue

// set new completion indicator
void SingleBox::setIndicator( bool isComplete )
{
    completionStatus = isComplete;
} // end function setIndicator

// return the status indicator
bool SingleBox::isComplete(  )
{
     return completionStatus;
}

// return the BoxValue
int SingleBox::getBoxValue(  )
{
    return boxValue;
} // end function getBoxValue

// set value to zero if in possibilities vector
void SingleBox::updatePoss( int number )
{
    int zeroCnt = 0;
    int value = 0;

    for ( int i = 0; i < possibilities.size(); i++ ) {
        if ( possibilities[ i ] == number ) {
            possibilities[ i ] = 0;
            break;
        }
    }

    for ( int j = 0; j < possibilities.size(); j++ ) {
        if ( possibilities[ j ] == 0 ) {
            zeroCnt++;
        }
        else {
            value = possibilities[ j ];
        }
    }

    if ( zeroCnt == possibilities.size() - 1 ) {
        boxValue = value;
        setIndicator( true );
    }
} // end function updateProbs

// print all data members of the class to the console
void SingleBox::debugPrinter(  )
{
    std::cout << std::endl;
    std::cout << "SingleBox class contains the following \n";
    std::cout << " - Completion status = " << completionStatus << std::endl;
    std::cout << " - NineBoxNumber     = " << nineBoxNumber << std::endl;
    std::cout << " - BoxValue          = " << boxValue << std::endl;
    std::cout << " - Possibilities     = [";

    for (int i = 0; i < possibilities.size(); i++) {
        std::cout << possibilities[i];
        if (i != possibilities.size() - 1) {
            std::cout << ", ";
        }
    }

    std::cout << "]\n" << std::endl;
} // end function debugPrinter
A.W.
  • 186
  • 1
  • 8
  • What do you mean *the code below never executes*? What happens? Is there an error in the compiler? Does XCode immediately throw you into debug mode to try and figure out what went wrong (AKA `gdb`)? – scohe001 Jul 30 '14 at 15:02
  • Please read [this post](http://stackoverflow.com/questions/4782757/rule-of-three-becomes-rule-of-five-with-c11) – Khouri Giordano Jul 30 '14 at 15:02
  • Xcode does not indicate any errors or warnings. It simply does not output any message to the console after the attempt to initialize the vector. – A.W. Jul 30 '14 at 15:06
  • 1
    I copy, pasted your program in a single file and then compiled and executed it : it seems to work fine for me. Have you tried to complie it outside of your IDE ? – Thomas Benard Jul 30 '14 at 15:23
  • No, I have not tried using the g++ compiler yet. But, I will go ahead and try doing it that way because that is valid point. – A.W. Jul 30 '14 at 15:31
  • Thank you Thomas Bernard. Running compiling and running the code from the command line with g++ enabled me to spot an error in my code. Apparently, assigning **possibilities = {1, 2, 3, 4, 5, 6, 7, 8, 9}** in my **SingleBox.h** file is incorrect. I just had to perform a total of nine push_back() operations and the code ran fine. The issue has now been resolved. – A.W. Jul 30 '14 at 15:53

1 Answers1

0

The Xcode IDE was not running a portion of my code because a breakpoint had been set. Running the code via the g++ compile verified that my code was operational, and removing the breakpoint from Xcode allowed my code to operate in the IDE.

A.W.
  • 186
  • 1
  • 8