开发者

Why this error? "no appropriate default constructor available"

Someone has given me the following C++ code snippet to try out - and now I have lost contact with them (its a long story). Anyway, it won't compile - I get an error

error C2512: 'mstream' : no appropriate default constructor available

Can anyone explain why, and what is 开发者_开发技巧needed to fix it.

class mstream : private ostream
{
  public:

  mstream& operator << (char *value)
  {
    printf ("[%s]\n", value);
    return *this;
  }
  mstream& operator << (int value)
  {
    printf ("[%u]\n", value);
    return *this;
  }

};
mstream g_mcout;

EDIT: Oops, missed this out...

ostream& mcout ()
{
  return g_mcout;
}
ostream& SgDebug ()
{
  return g_mcout;
}

FYI: the reason for this strange looking code is all to do with merging a C++ with a C program. The printf()'s will actually be changed to my_printf()'s which do a wide variety of custom things.


ostream has no default constructor; the implicitly created default constructor for mstream is invalid because of it. You need to provide the ostream with a stream buffer:

class mstream : private ostream
{
public:
    mstream() :
    ostream(/* whatever you want */)
    {}

    /* Maybe this is more appropriate:

    mstream(std::streambuf* pBuffer) :
    ostream(pBuffer)
    {}

    */

    // ...
};

So it can construct. What you put there depends on what you're trying to do.


mstream inherits from ostream, which is a typedef for class template basic_ostream specialized on char. Since you don't define a constructor for mstream the compiler will attempt to provide one using default constructors for ostream to initialize the base class.

However, basic_ostream has no default constructor (with no parameters), so you must provide an mstream constructor with suitable inputs for the base class constructor. Your choices of how to construct the base class are :

explicit basic_ostream(
    basic_streambuf<_Elem, _Tr> *_Strbuf,
    bool _Isstd = false
);
basic_ostream(
    basic_ostream&& _Right
);

and the first is the most sensible choice for you to use, for example like so:

class mstreambuffer : public streambuf
{
public:
    mstreambuffer() : streambuf()
    {
    }
};

class mstream : private ostream
{
public:
    mstream(mstreambuffer* buff) : ostream(buff) {}
};

int main(void)
{
    mstreambuffer buff;
    mstream g_mcout(&buff);
    g_mcout << 32768;
    return 0;
}

mstreambuffer is necessary because streambuf is abstract.

btw besides being uncompilable, this code also uses CRT (printf) instead of more usual C++ output via cout, so it's doubly suspect. Prefer this:

  mstream& operator << (int value)
  {
    std::cout << value << std::endl;
    return *this;
  }


Your parent class (ostream) doesn't have a default constructor, so your mstream class's constructor must manually construct the ostream parent as well.


You need to add a default constructor as said because you're trying to create an instance.

class mstream : private ostream
{
    public:
    mstream()
    {
    }
    ...
}

See http://www.cplusplus.com/reference/iostream/ostream/ostream/ for details of the arguments to pass.


std::ostream is a typedef:

typedef basic_ostream<char, char_traits<char> > ostream;

Then std::basic_ostream<> does not have a default contstructor, only this:

explicit basic_ostream(basic_streambuf<Elem, Tr> *strbuf);

So you need to call the constructor of the base class explicitly L

mstream(): ostream( pointer_to_some_implementation_of_streambuf ) { ... }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜