friend does not match any template declaration problem

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 }
 
B

brian tyler

Have you read this?

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16

Personally I find it mush easier to define all template classes and
functions inline where they are declared, that way you don't run as
many syntactical problems (at least not in the declarations
themselves). For example try defining and using an overloaded friend
operator of a nested template class somewhere other than where you
declared the function if you want some fun (or not!).

The STL and Boost libraries adopt this approach (inline definitions),
so I think it is hard to argue that it is bad style. When I first
started writing template functions / classes I was in the "separate
interface from implementation" mindset, over time I found it was more
hassle than it was worth. Anyway that's just my opinion.

Brian.
 
B

brian tyler

Have you read this?

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16

Personally I find it mush easier to define all template classes and
functions inline where they are declared, that way you don't run as
many syntactical problems (at least not in the declarations
themselves). For example try defining and using an overloaded friend
operator of a nested template class somewhere other than where you
declared the function if you want some fun (or not!).

The STL and Boost libraries adopt this approach (inline definitions),
so I think it is hard to argue that it is bad style. When I first
started writing template functions / classes I was in the "separate
interface from implementation" mindset, over time I found it was more
hassle than it was worth. Anyway that's just my opinion.

Brian.

^^ Apologies for misspellings and missing words

mush = much
don't run as many = don't run into as many
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top