Note that there are some explanatory texts on larger screens.

plurals
  1. POInconsistency in my design of C++
    text
    copied!<p>I am currenty doing a Sudoku game in c++. For that , I defined many classes including, <code>class Pos</code> for position, and <code>class Error</code> for maintaining errors.</p> <p>Now, in <code>class Pos</code>,</p> <pre><code>class Pos { int x; int y; public: Pos(); Pos(Pos const&amp;); Pos(int,int); void setPos(int,int); void setx(int); void sety(int); int getx() const ; int gety() const ; string getPosReport(); virtual ~Pos(); }; </code></pre> <p><strong>What I have done:</strong> I have restricted the value for x and y to lie between 0-9 by writing <code>setx</code> and <code>sety</code> as,</p> <pre><code>void Pos::setx(int x){ if((x &lt; 0) &amp; (x &gt; 9)) throw Exception("Invalid Position"); else this-&gt;x = x; } void Pos::sety(int y){ if((y &lt; 0) &amp; (y &gt; 9)){ throw Exception("Invalid Position"); } else this-&gt;y = y; } </code></pre> <p><strong>My Problem:</strong> How <code>Pos()</code> must be defined. </p> <p><strong>What I have worked:</strong></p> <ol> <li><p>Removing default constructor, didnt helped me because,</p> <pre><code>class Error { string errmsg; Pos pos1,pos2; // Pos() must be called!! default constructor cannot be removed! bool isError; // ...... } </code></pre></li> <li><p>I can initialize x and y to -1 in <code>Pos()</code> , but I feel that will bring instability in my code.</p></li> <li><p>I can have <code>int* x</code> and <code>int* y</code> in <code>class Pos</code> such that I can have x = y = NULL, but I feel Pointers will make my code relatively complex than now.</p> <p>so,it will be better if some one gives a solution which stimulates the scenario like x = y = N/A (not applicable) </p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload