Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22113

How can I construct objects within constructor and assign the reference?

$
0
0

I have a class called Tree with three instance variables called board (Board), root (Node) and end (Point). Additionally it has a constructor of six parameters. I want to create the objects inside the constructor and assign them to the references (the instance variables).

When I try to assign the root I get function "Node::operator=(const Node &)" (declared implicitly) cannot be referenced -- it is a deleted function error.

I did some research and it seems you have to construct reference variables in the constructor initializer, but I cannot figure out how without changing the parameters. I know (kinda) how the initalizer works, but the problem is in the constructor block. I would like to create the objects in the block and assign, since I cannot directly use the initializer without changing the parameters to the objects itself. If this code was Java then it would work.

/* HEADER FILE */
class Tree {
public:
    Tree(int boardHeight, int boardWidth, int knightStartXPosition,
    int knightStartYPosition, int knightEndXPosition, int knightEndYPosition);

private:
    Board& board; // Board has int height and int width.
    Node& root; // Node has location (Point) and end (Point)
    Point& end; // A point is a plane vector of x and y.

    void jumpNodes(Node* node); // not used yet
};
/* IMPLEMENTATION FILE */
Tree::Tree(int boardHeight, int boardWidth, int knightStartXPosition,
    int knightStartYPosition, int knightEndXPosition, int knightEndYPosition) {
        Point start = Point(knightEndXPosition, knightEndYPosition);
        Point end = Point(knightStartXPosition, knightStartYPosition);
        Node root = Node(start, end);

        this->board = Board(boardHeight, boardWidth);
        this->root = root; // this line gives the error
        this->end = end;
};

The expected solution is the constructor works with given parameters, but if it is not possible, I need to inputs and structure.


Viewing all articles
Browse latest Browse all 22113

Trending Articles