Navigate classes when constructing and initializing

Hi!
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  #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:
1
2
3
4
5
6
7
8
9
10
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<Node>&' "
"default-initialization of reference is not allowed "

What should I change to keep the cell nodes referring to the mesh nodes
Last edited on
References must point to (i.e. reference, as in the verb) a valid object. If a potentially valid state of your Cell object is that 'nodes' does not reference a valid vector<Node>, then it cannot be a reference.

If you need a vector<Cell>, you can't pre-initialize them with "null references". (Because you can't legally have null references in C++).

You can, however, do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Example program
#include <iostream>
#include <vector>

struct Foo {
    Foo(int& bar)
    : bar(bar) { }
    
    int& bar;  
};

int main()
{
    int bar = 42;
    
    std::vector<Foo> foos;
    foos.reserve(10);
    
    foos.push_back(Foo(bar));
    foos.push_back(Foo(bar));
    foos.push_back(Foo(bar));
    
    foos[2].bar = 43;
   
    std::cout << foos[1].bar << '\n'; // 43
}


foos.size() is 3. foos.capacity() is 10 or more.

In the above, you do not need a default constructor, because you never default construct any of the objects.
Last edited on
I'm trying to build a structured data structure for CFD


If that were the case then you just need an array.

What you appear to be trying to build is an unstructured mesh. There is an enormous difference!
Topic archived. No new replies allowed.