friend does not match any template declaration problem

Discussion in 'C++' started by Klaus, Mar 31, 2008.

  1. Klaus

    Klaus Guest

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

    Thanks
    Klaus
     
    Klaus, Mar 31, 2008
    #1
    1. Advertising

  2. Klaus

    brian tyler Guest

    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.
     
    brian tyler, Mar 31, 2008
    #2
    1. Advertising

  3. Klaus

    brian tyler Guest

    On Mar 31, 3:51 pm, brian tyler <> wrote:
    > 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
     
    brian tyler, Mar 31, 2008
    #3
  4. Klaus

    Klaus Guest

    Klaus, Mar 31, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john smith
    Replies:
    1
    Views:
    1,922
    Victor Bazarov
    Aug 10, 2003
  2. blueblueblue2005
    Replies:
    6
    Views:
    1,017
    blueblueblue2005
    Jul 19, 2005
  3. Alex Buell
    Replies:
    2
    Views:
    670
    Alex Buell
    Feb 21, 2006
  4. Joseph Turian
    Replies:
    2
    Views:
    866
    Joseph Turian
    Mar 22, 2007
  5. A L
    Replies:
    1
    Views:
    516
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page