C ++: Sudoku (copy)

I am new to C ++ and have encountered a problem while doing homework (Sudoku).

Instruction: "you need to create a new board that is a copy of the current board (use the copy constructor and select the board from the heap with a new one).

I tried (this is wriiten in board.cc):

#include "board.h" // Search for a solution, returns NULL if no solution found Board* Board::search(void) { Board b = new Board(&this); ... return b; } 

Received msg error:

 lvalue required as unary '&' operand. 

I also tried:

 Board* Board::search(void) { Board b; Board *b3; b3 = &b; ... return b3; } 

This is not a problem when comp. but it does not work at startup.

How to do it? Actually need help here, thanks!

Here is the code for board.h:

 class Board { private: Field fs[9][9]; // All fields on board public: // Initialize board Board(void) { // Uses the default constructor of Field! } // Copy board Board(const Board& b) { for(int i = 0; i < 9; i++){ for(int j = 0; j < 9; j++){ fs[i][j] = b.fs[i][j]; } } } // Assignment operator for board Board& operator=(const Board& b) { if(this != &b){ for(int i = 0; i < 9; i++){ for(int j = 0; j < 9; j++){ fs[i][j] = b.fs[i][j]; } } } return *this; } .... 

Full instructions can be found here: http://www.kth.se/polopoly_fs/1.136980!/Menu/general/column-content/attachment/2-2.pdf

Code: http://www.kth.se/polopoly_fs/1.136981!/Menu/general/column-content/attachment/2-2.zip

+4
source share
2 answers

Edit:

 Board b = new Board(&this); 

in

 Board *b = new Board(*this); 
+7
source
 Board b = new Board(&this); 

This line has two errors, the first of which is that if you select new , the type will be Board * , not just Board . The second mistake is that if you want to use the copy constructor, you must pass the element specified by this , and not the address this :

 Board * b = new Board( *this ); 
+5
source

Source: https://habr.com/ru/post/1384689/


All Articles