K
Klaus
Hi all,
I wrote the following little program which works if I do not use the
friend declaration, but I want private class members with the friend
declaration. I need help for the correct template syntax I think
The compiler (gcc) throws the error:
1 || g++ -O2 main.cpp -o go
2 || main.cpp: In instantiation of 'oma<int>':
3 main.cpp|47| instantiated from
here
4 main.cpp|29| error: template-id 'operator<< <>' for 'Logger&
operator<<(Logger&, const oma<int>&)' does not match any template
declaration
5 || make: *** [go] Error 1
the program:
-------------------------------------------------------
1 #include <iostream>
2
3 using namespace std;
4
5 class Logger {
6 private:
7 char buffer[1024][2]; //2 Buffer fuer Nachrichten
8 unsigned int offset[2]; //2 Offsets auf die Buffer zum
Schreiben
9 int actWriteBuffer; //wo wird aktuell geschrieben
10
11 void write( char *s); //schreibt einen NULL terminierten
String in den freien Buffer
12 void flush();
13 };
14
15 static inline Logger &operator<< (Logger& l, Logger& (*fn)
(Logger&)) {
16 return fn(l);
17 }
18
19 template <class T>
20 class oma {
21 // private:
22 public:
23 Logger &(*f)(Logger&, T);
24 T p;
25
26 public:
27 oma(Logger& (*_f)(Logger&, T), T _p): f(_f),p(_p) {}
28
29 friend Logger& operator << <>(Logger& l, const oma&);
30 };
31
32
33 template <class T>
34 inline Logger& operator << (Logger& l, const oma<T>& o) {
35 return (o.f)(l,o.p);
36 }
37
38 Logger& mani1(Logger& l) {
39 cout << "Mani 1 called" << endl;
40 }
41
42 Logger& _mani3(Logger &l, int i) {
43 cout << "Logger with param " << i << endl;
44 return l;
45 }
46
47 const oma<int> mani3(int n) { return oma<int>(_mani3, n); }
48
49 Logger logger;
50
51 int main () {
52 logger << mani1 ;
53 logger << mani3(19);
54 }
I wrote the following little program which works if I do not use the
friend declaration, but I want private class members with the friend
declaration. I need help for the correct template syntax I think
The compiler (gcc) throws the error:
1 || g++ -O2 main.cpp -o go
2 || main.cpp: In instantiation of 'oma<int>':
3 main.cpp|47| instantiated from
here
4 main.cpp|29| error: template-id 'operator<< <>' for 'Logger&
operator<<(Logger&, const oma<int>&)' does not match any template
declaration
5 || make: *** [go] Error 1
the program:
-------------------------------------------------------
1 #include <iostream>
2
3 using namespace std;
4
5 class Logger {
6 private:
7 char buffer[1024][2]; //2 Buffer fuer Nachrichten
8 unsigned int offset[2]; //2 Offsets auf die Buffer zum
Schreiben
9 int actWriteBuffer; //wo wird aktuell geschrieben
10
11 void write( char *s); //schreibt einen NULL terminierten
String in den freien Buffer
12 void flush();
13 };
14
15 static inline Logger &operator<< (Logger& l, Logger& (*fn)
(Logger&)) {
16 return fn(l);
17 }
18
19 template <class T>
20 class oma {
21 // private:
22 public:
23 Logger &(*f)(Logger&, T);
24 T p;
25
26 public:
27 oma(Logger& (*_f)(Logger&, T), T _p): f(_f),p(_p) {}
28
29 friend Logger& operator << <>(Logger& l, const oma&);
30 };
31
32
33 template <class T>
34 inline Logger& operator << (Logger& l, const oma<T>& o) {
35 return (o.f)(l,o.p);
36 }
37
38 Logger& mani1(Logger& l) {
39 cout << "Mani 1 called" << endl;
40 }
41
42 Logger& _mani3(Logger &l, int i) {
43 cout << "Logger with param " << i << endl;
44 return l;
45 }
46
47 const oma<int> mani3(int n) { return oma<int>(_mani3, n); }
48
49 Logger logger;
50
51 int main () {
52 logger << mani1 ;
53 logger << mani3(19);
54 }