簡體   English   中英

在Xcode IDE中用C ++初始化我自己的類的向量

[英]Initializing a vector of my own class in C++ in Xcode IDE

我當前遇到的問題如下:

我試圖在僅包含SingleBox對象的main.cpp文件中構造一個向量(SingleBox是我定義的類),但是,當我嘗試填充該向量或調整其大小時,該向量從不包含任何SingleBox對象,並且嘗試填充或調整矢量大小的下面的代碼從未在main.cpp文件中執行。

在此先感謝您提供協助解決此問題的任何人。

文件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;
}

文件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__) */

文件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

Xcode IDE沒有運行我的代碼的一部分,因為已經設置了斷點。 通過g ++編譯器運行代碼可驗證我的代碼是否可運行,並且從Xcode刪除斷點可讓我的代碼在IDE中運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM