Resolving Cyclic Dependencies in Templates

I am having an issue with cyclic dependencies with template classes. I have something similar to the following,

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
// XYZ.hxx
template<typename T>
class YZX;

template<typename T>
class ZXY;

template<typename T>
class XYZ
{
    XYZ(T x, T y, T z)
    {
        data[0] = x;
        data[1] = y;
        data[2] = z;
    }
    
    XYZ(YZX<T> yzx) :
     XYZ(yzx.x(),yzx.y(),yzx.z())
    {}
    
    XYZ(ZXY<T> zxy) :
     XYZ(zxy.x(),zxy.y(),zxy.z())
    {}


    T x() {return data[0];}
    T y() {return data[1];}
    T z() {return data[2];}

    T data[3];
}



// YZX.hxx
template<typename T>
class XYZ;

template<typename T>
class ZXY;

template<typename T>
class YZX
{
    YZX(T y, T z, T x)
    {
        data[0] = y;
        data[1] = z;
        data[2] = x;
    }
    
    YZX(XYZ<T> xyz) :
     YZX(xyz.y(),xyz.z(),xyz.x())
    {}
    
    YZX(ZXY<T> zxy) :
     XYZ(zxy.y(),zxy.z(),zxy.x())
    {}
    
    T x() {return data[2];}
    T y() {return data[0];}
    T z() {return data[1];}

    T data[3];
}



// ZXY.hxx
template<typename T>
class XYZ;

template<typename T>
class YZX;

template<typename T>
class ZXY
{
    ZXY(T z, T x, T y)
    {
        data[0] = z;
        data[1] = x;
        data[2] = y;
    }

    ZXY(XYZ<T> xyz) :
     ZXY(xyz.z(),xyz.x(),xyz.y())
    {}
    
    ZXY(YZX<T> yzx) :
     ZXZ(yzx.z(),yzx.x(),yzx.y())
    {}
    
    T x() {return data[1];}
    T y() {return data[2];}
    T z() {return data[0];}

    T data[3];
}


The forward declarations do not work. I have tried breaking the definitions out of the declaration and including the relevant hxx file after declaring the class, but no luck either. Any help would be appreciated. Thanks
Last edited on
> The forward declarations do not work
error messages.

> breaking the definitions out of the declaration
yes, you'lll probably need to do that

> but no luck either.
testcase and error messages..


> ZXY(XYZ xyz)
Try ZXY(const XYZ &xyz) instead (same for the others).
You also need to make the methods const-correct.
Topic archived. No new replies allowed.