Navigate classes when constructing and initializing

20 Views Asked by At

I'm trying to build a structured data structure for a CFD toll. One main component is the Mesh. Thus, I'm creating a Mesh class with Node, Edge, and Cell classes. Edges and Cells are made of the same nodes. I want to create the mesh nodes and make the Edges and cells nodes point at or refer to the same nodes of the Mesh. I cannot understand how to navigate using pointers and references while calling the constructor and the initialization list. I hope this is clear. The following is part of the code.

  #include <iostream>
using namespace std;

#include <vector>

using namespace std;

class Node {
public:
    double X, Y;
    Node():X(0),Y(0){};
    Node(double x, double y) : X(x), Y(y) {}

};

class Cell {
public:
    int I, J;
    vector<Node> nodes;

    Cell() : I(0), J(0), nodes() {}
        //constructor
    Cell(int i, int j,  vector<Node>& cell_nodes) :
        I(i), J(j), nodes(cell_nodes) {}

    // copy assignment operator
    Cell& operator=(const Cell& other) {
        if (this != &other) {
            I = other.I;
            J = other.J;
            nodes = other.nodes;
        }
        return *this;
    } 


    //----------------------
    double x() const {
        double x_sum = 0.0;
        for (const auto& node : nodes) {
            x_sum += node.X;
        }
        return x_sum / nodes.size();
    }
    double y() const {
        double y_sum = 0.0;
        for (const auto& node : nodes) {
            y_sum += node.Y;
        }
        return y_sum / nodes.size();
    }
    
    double area() const {
        int n = nodes.size();
        double area_sum = 0.0;
        for (int i=0; i<n; i++) {
            int j = (i+1) % n;
            area_sum += nodes[i].X * nodes[j].Y - nodes[j].X * nodes[i].Y;
        }
        return 0.5 * area_sum;
    }


};

class Edge{
    private:
        /* data */
    public:
        Edge
        (/* args */);
        ~Edge
        ();
};



class Mesh2D {
public:
    double L, H;
    int NX, NY;
    vector<Node> nodes;
    vector<Cell> cells;
    Mesh2D(double length, double height, int nx, int ny) :
        L(length), H(height), NX(nx), NY(ny)
    {
        // Allocate memory for the nodes and cells
        nodes.resize((NX+1)*(NY+1));
        cells.resize(NX*NY);
        
        // Initialize the nodes
        double dx = L/NX;
        double dy = H/NY;
        for (int j=0; j<=NY; j++)
        {
            for (int i=0; i<=NX; i++)
            {
                nodes[i + j*(NX+1)] = Node(i*dx, j*dy);
            }
        }
        
        // Initialize the cells
        for (int j=0; j<NY; j++)
        {
            for (int i=0; i<NX; i++)
            {
                int n00 = i + j*(NX+1);
                int n10 = (i+1) + j*(NX+1);
                int n01 = i + (j+1)*(NX+1);
                int n11 = (i+1) + (j+1)*(NX+1);
                vector<Node> cell_nodes = { nodes[n00], nodes[n10], nodes[n11], nodes[n01] };
                cells[i + j*NX] = Cell(i, j, cell_nodes);
            }
        }
    }
};

int main (){

    Mesh2D mesh(10.0, 2.0, 10, 2);

        cout<<"Hello there!";
    return 0;
}

I want the Cell class to be something like:

class Cell {
public:
    int I, J;
    vector<Node>& nodes;

    Cell() : I(0), J(0), nodes() {}
        //constructor
    Cell(int i, int j,  vector<Node>& cell_nodes) :
        I(i), J(j), nodes(cell_nodes) {}
};

But I'm getting the following error messages with GCC compiler, VS Code IDE on win11: "value-initialization of reference type 'std::vector&' " "default-initialization of reference is not allowed "

What should I change to keep the cell nodes referring to the mesh nodes

0

There are 0 best solutions below