N
Nomak
Hello all,
i have two template classes which needs each other. I tried to write
some fwd decl / decl / impl in a good way but i can't get it to
compile.
Explanations:
- .hh files are for declaration (template or not)
- .hxx files are template impl
I would like to avoid some .ixx file for forward declaration. I'm
searching for a clever way.
Here are the files:
====> a.hh <====
1 /*
2 * A decl
3 */
4 #ifndef A_HH
5 #define A_HH
6
7 // fwd decl
8 class ostream;
9 class B;
10
11
12 template <typename T>
13 class A
14 {
15 public:
16 A(const T & t);
17 A(const B<T> & b);
18
19 void dump(ostream & os) const;
20 };
21
22 #endif // ! A_HH
23
====> a.hxx <====
1 /*
2 * A impl
3 */
4 #ifndef A_HXX
5 #define A_HXX
6
7 // need self decl
8 #include "a.hh"
9
10 // need impl
11 #include <ostream>
12 #include "b.hxx"
13
14 template <typename T>
15 A<T>::A(const T & t)
16 {
17 }
18
19 template <typename T>
20 A<T>::A(const B<T> & b)
21 {
22 }
23
24 template <typename T>
25 A<T>::dump(ostream & os)
26 {
27 os << "dump";
28 }
29
30 #endif // ! A_HXX
====> b.hh <====
1 /*
2 * B decl
3 */
4 #ifndef B_HH
5 #define B_HH
6
7 // fwd decl
8 using std:stream;
9 using A;
10
11
12 template <typename T>
13 class B
14 {
15 public:
16 B(const T & t);
17 B(const A<T> & b);
18
19 void dump(ostream & os) const;
20 };
21
22 #endif // ! B_HH
====> b.hxx <====
1 /*
2 * B impl
3 */
4 #ifndef B_HXX
5 #define B_HXX
6
7 // need self decl
8 #include "b.hh"
9
10 // need impl
11 #include <ostream>
12 #include "a.hxx"
13
14 template <typename T>
15 B<T>::B(const T & t)
16 {
17 }
18
19 template <typename T>
20 B<T>::B(const A & b)
21 {
22 }
23
24 template <typename T>
25 B<T>::dump(ostream & os)
26 {
27 os << "dump";
28 }
29
30 #endif // ! B_HXX
====> main.cc <====
1 /*
2 * Program
3 */
4
5 // need impl
6 #include "a.hxx"
7 #include "b.hxx"
8
9
10 int
11 main()
12 {
13 {
14 A<int> a1(1);
15
16 B<int> b1(2);
17 B<int> b2(a1);
18
19 A<int> a2(b1);
20 }
21 return 0;
22 }
23
$ g++-3.4.0 -Wall main.cc
In file included from a.hxx:8,
from main.cc:6:
a.hh:17: error: `B' is not a template
In file included from b.hxx:8,
from a.hxx:12,
from main.cc:6:
b.hh:8: error: `ostream' is already declared in this scope
b.hh:9: error: expected nested-name-specifier before "A"
b.hh:9: confused by earlier errors, bailing out
Is there a standard way to handle that?
TIA
NB: i've read several times "inline" to indicate that template
implementation must be included in the same compilation unit (included
from the same .cc) as the declaration. I thought inline was in fact
"implicit inlining" which is having the decl and impl mixed (in the
class decl). Who's right?
i have two template classes which needs each other. I tried to write
some fwd decl / decl / impl in a good way but i can't get it to
compile.
Explanations:
- .hh files are for declaration (template or not)
- .hxx files are template impl
I would like to avoid some .ixx file for forward declaration. I'm
searching for a clever way.
Here are the files:
====> a.hh <====
1 /*
2 * A decl
3 */
4 #ifndef A_HH
5 #define A_HH
6
7 // fwd decl
8 class ostream;
9 class B;
10
11
12 template <typename T>
13 class A
14 {
15 public:
16 A(const T & t);
17 A(const B<T> & b);
18
19 void dump(ostream & os) const;
20 };
21
22 #endif // ! A_HH
23
====> a.hxx <====
1 /*
2 * A impl
3 */
4 #ifndef A_HXX
5 #define A_HXX
6
7 // need self decl
8 #include "a.hh"
9
10 // need impl
11 #include <ostream>
12 #include "b.hxx"
13
14 template <typename T>
15 A<T>::A(const T & t)
16 {
17 }
18
19 template <typename T>
20 A<T>::A(const B<T> & b)
21 {
22 }
23
24 template <typename T>
25 A<T>::dump(ostream & os)
26 {
27 os << "dump";
28 }
29
30 #endif // ! A_HXX
====> b.hh <====
1 /*
2 * B decl
3 */
4 #ifndef B_HH
5 #define B_HH
6
7 // fwd decl
8 using std:stream;
9 using A;
10
11
12 template <typename T>
13 class B
14 {
15 public:
16 B(const T & t);
17 B(const A<T> & b);
18
19 void dump(ostream & os) const;
20 };
21
22 #endif // ! B_HH
====> b.hxx <====
1 /*
2 * B impl
3 */
4 #ifndef B_HXX
5 #define B_HXX
6
7 // need self decl
8 #include "b.hh"
9
10 // need impl
11 #include <ostream>
12 #include "a.hxx"
13
14 template <typename T>
15 B<T>::B(const T & t)
16 {
17 }
18
19 template <typename T>
20 B<T>::B(const A & b)
21 {
22 }
23
24 template <typename T>
25 B<T>::dump(ostream & os)
26 {
27 os << "dump";
28 }
29
30 #endif // ! B_HXX
====> main.cc <====
1 /*
2 * Program
3 */
4
5 // need impl
6 #include "a.hxx"
7 #include "b.hxx"
8
9
10 int
11 main()
12 {
13 {
14 A<int> a1(1);
15
16 B<int> b1(2);
17 B<int> b2(a1);
18
19 A<int> a2(b1);
20 }
21 return 0;
22 }
23
$ g++-3.4.0 -Wall main.cc
In file included from a.hxx:8,
from main.cc:6:
a.hh:17: error: `B' is not a template
In file included from b.hxx:8,
from a.hxx:12,
from main.cc:6:
b.hh:8: error: `ostream' is already declared in this scope
b.hh:9: error: expected nested-name-specifier before "A"
b.hh:9: confused by earlier errors, bailing out
Is there a standard way to handle that?
TIA
NB: i've read several times "inline" to indicate that template
implementation must be included in the same compilation unit (included
from the same .cc) as the declaration. I thought inline was in fact
"implicit inlining" which is having the decl and impl mixed (in the
class decl). Who's right?