variable can't be defined in namespace

Hello forum,
I defined a header file name Cell.h as bellow,

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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#pragma once
#ifndef _CELL_H_
#define _CELL_H_

#include <iostream>
#include <array>
class Cell
{
public:
    Cell();
    ~Cell();

    static std::array<int, 9> ex;
    static std::array<int, 9> ey;
    static std::array<double, 9> coeff;
    std::array<double, 9> density;

    enum Direction
    {
        none = 0,
        N,
        NE,
        E,
        SE,
        S,
        SW,
        W,
        NW
    };

    void Init();

    inline static std::pair<int, int> GetNextPosition(Direction direction, int x, int y)
    {
        return std::make_pair<int, int>(x + ex[direction], y + ey[direction]);
    }

    inline static Direction Reverse(Direction dir)
    {
        switch (dir)
        {
        case Direction::N:
            return Direction::S;
        case Direction::S:
            return Direction::N;
        case Direction::W:
            return Direction::E;
        case Direction::E:
            return Direction::W;
        case Direction::NE:
            return Direction::SW;
        case Direction::SE:
            return Direction::NW;
        case Direction::NW:
            return Direction::SE;
        case Direction::SW:
            return Direction::NE;
        }

        return Direction::none;
    }

    inline static Direction ReflectVert(Direction dir)
    {
        switch (dir)
        {
        case Direction::N:
            return Direction::S;
        case Direction::S:
            return Direction::N;
        case Direction::W:
            return Direction::W;
        case Direction::E:
            return Direction::E;
        case Direction::NE:
            return Direction::SE;
        case Direction::SE:
            return Direction::NE;
        case Direction::NW:
            return Direction::SW;
        case Direction::SW:
            return Direction::NW;
        }

        return Direction::none;
    }

    inline double Density() const
    {
        double tDensity = 0;

        for (int i = 0; i < 9; ++i)
            tDensity += density[i];

        return tDensity;
    }

    inline std::pair<double, double> Velocity() const
    {
        double tDensity = 0;
        double vx = 0;
        double vy = 0;

        for (int i = 0; i < 9; ++i)
        {
            tDensity += density[i];
            vx += ex[i] * density[i];
            vy += ey[i] * density[i];
        }

        if (tDensity < 1E-14) return std::make_pair<double, double>(0, 0);

        return std::make_pair<double, double>(vx / tDensity, vy / tDensity);
    }

    
    inline std::array<double, 9> Equilibrium(double accelXtau, double accelYtau) const
    {
        std::array<double, 9> result;

        double totalDensity = density[0];
        double vx = ex[0] * density[0];
        double vy = ey[0] * density[0];

        for (int i = 1; i < 9; ++i)
        {
            totalDensity += density[i];
            vx += ex[i] * density[i];
            vy += ey[i] * density[i];
        }

        vx /= totalDensity;
        vy /= totalDensity;

        vx += accelXtau;
        vy += accelYtau;

        const double v2 = vx * vx + vy * vy;

        static const double coeff1 = 3.;
        static const double coeff2 = 9. / 2.;
        static const double coeff3 = -3. / 2.;

        for (int i = 0; i < 9; ++i)
        {
            const double term = ex[i] * vx + ey[i] * vy;

            result[i] = coeff[i] * totalDensity * (1. + coeff1 * term + coeff2 * term * term + coeff3 * v2);
        }

        return std::move(result);
    }

    inline void Collision(double accelXtau, double accelYtau, double tau)
    {
        const std::array<double, 9> equilibriumDistribution = Equilibrium(accelXtau, accelYtau);

        for (int i = 0; i < 9; ++i)
            density[i] -= (density[i] - equilibriumDistribution[i]) / tau;
    }

};
#endif 

and the main file Cell.cpp as follows;

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
#include "cell.h"

namespace LatticeBoltzmann {


    const double c0 = 4. / 9.;
    const double c1 = 1. / 9;
    const double c2 = 1. / 36.;
    // 0, N, NE,E, SE,  S, SW,  W, NW
    std::array<int, 9> Cell::ex = std::array<int, 9>{ {0, 0, 1, 1, 1, 0, -1, -1, -1} };
    std::array<int, 9> Cell::ey = std::array<int, 9>{ {0, 1, 1, 0, -1, -1, -1, 0, 1} };

    std::array<double, 9> Cell::coeff = std::array<double, 9>{ { c0, c1, c2, c1, c2, c1, c2, c1, c2 } };



    Cell::~Cell()
    {
    }

    void Cell::Init()
    {
        for (int i = 0; i < 9; ++i)
            density[i] = coeff[i];
    }
}


now I don't understand why the compiler says class member function cannot be defined in the current zone. The error happens in lines 10, 11, 13, 17, 21.
Last edited on
Define the member functions in the same namespace as the class definition.
Change the source file to read:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
namespace LatticeBoltzmann {
    const double c0 = 4. / 9.;
    const double c1 = 1. / 9;
    const double c2 = 1. / 36.;
}

// 0, N, NE,E, SE,  S, SW,  W, NW
std::array<int, 9> Cell::ex = std::array<int, 9>{ {0, 0, 1, 1, 1, 0, -1, -1, -1} };
std::array<int, 9> Cell::ey = std::array<int, 9>{ {0, 1, 1, 0, -1, -1, -1, 0, 1} };

using namespace LatticeBoltzmann;
std::array<double, 9> Cell::coeff = std::array<double, 9>{ { c0, c1, c2, c1, c2, c1, c2, c1, c2 } };

Cell::~Cell()
{
}

void Cell::Init()
{
    for (int i = 0; i < 9; ++i)
        density[i] = coeff[i];
}
Thanks for the answer. I appreciate it.
Topic archived. No new replies allowed.