Re: [C++] std::ostream::rdbuf()

From: Martin Henne (martin.henne_at_web.de)
Date: 10/25/03


Date: Sat, 25 Oct 2003 20:00:26 +0200

Mike Wahler wrote:

> Show us exactly what you tried, and define 'doesn't work'.

This is the compile error:

 no match for `MyOstream& << <unknown type>' operator

Below is the code (as posted by Stefan before _plus_
the one line in main() that causes this error).

// Code example

 #include <iostream>

class setimportance {
  int imp;
public:
  setimportance(int i):imp(i) {}
  int get_importance() const {return imp;}
};

class MyOstream {
  std::ostream & out;
  int verbosity;
  int importance;
public:
  MyOstream(std::ostream& os)
    : out(os),
    verbosity(0),
    importance(0)
  {
  }
  void set_verbosity(int v) {
    verbosity=v;
  }
  friend MyOstream & operator << (MyOstream & o, setimportance & imp);

  template <typename T>
  MyOstream & operator << (T const & item) {
    if (importance<verbosity)
      out << item;
    return *this;
  }
};

template <>
MyOstream & MyOstream::operator << (setimportance const & imp) {
  importance=imp.get_importance();
  return *this;
}

int main() {
  MyOstream out (std::cout);
  for(int v=1;v<3;++v) {
    out.set_verbosity(v);
    std::cout << "verbosity set to " << v << std::endl;
    out << setimportance(0) << "Hah ... I dont care about verbosity!\n";
    out << setimportance(1) << "Uh ... but I have to!\n";

    out << "This endl causes a compilation error" << std::endl;

  }
}

// End of Code

-- 
martin dot henne at web dot de


Relevant Pages

  • Re: why does adding 2 bytes together result in an int?
    ... This is because the devleopers of the platform have decided that the return ... compile error: Cannot implicitly convert type 'int' to 'byte'. ... I need to convert ttl into an int? ...
    (microsoft.public.dotnet.languages.csharp)
  • [-mm patch] fs/cifs/cifsproto.h: remove #ifdef around small_smb_init_no_tc() prototype
    ... Let's hope gcc guesses the prototye of the function right. ... this patch has turned a compile error into a nasty runtime ... extern int CIFS_SessSetup(unsigned int xid, struct cifsSesInfo *ses, ...
    (Linux-Kernel)
  • Re: why does adding 2 bytes together result in an int?
    ... byte val2 = 23; ... compile error: Cannot implicitly convert type 'int' to 'byte'. ... I need to convert ttl into an int? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: variable trouble!
    ... Because out parameters are NOT required to be initialized prior to being ... passed (the only difference between out and ref parameters is that ref ... public static int Main ... I get this compile error mention above.... ...
    (microsoft.public.dotnet.languages.csharp)

Loading