我是 C++ 新手,在做作业(数独)时遇到问题。
指令说:“你必须创建一个新的板作为当前板的副本(使用复制构造函数并从堆中分配板与新的)。”
我试过(它写在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;
}
收到错误消息:
lvalue required as unary '&' operand.
我也试过:
Board* Board::search(void) {
Board b;
Board *b3;
b3 = &b;
...
return b3;
}
这在comp时没有问题。但它在运行时也不起作用。
怎么做?这里真的需要帮助,谢谢!
这是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;
}
....
完整的说明可以在这里找到: http ://www.kth.se/polopoly_fs/1.136980!/Menu/general/column-content/attachment/2-2.pdf
代码:http ://www.kth.se/polopoly_fs/1.136981!/Menu/general/column-content/attachment/2-2.zip