0

I'm trying to create a memory stream object to write to a block of memory as if it's a file (using the convenient functionality provided by std::istream). The basic idea is to create a bunch of wrappers for the std::streambuf class.

The issue seems to be that I'm using the std::istream constructor incorrectly. C++ is complaining that I'm referencing a deleted function.

I'm not quite sure what's wrong with the syntax.

inputStream.h

struct IMemBuf : std::streambuf
{
    IMemBuf(const char* base, size_t size);
};

struct IMemStream : virtual IMemBuf, std::istream
{
    IMemStream(const char* base, size_t size);
};

inputStream.cpp

IMemBuf::IMemBuf(const char* base, size_t size)
{
    char* p(const_cast<char*>(base));
    this->setg(p, p, p + size);

}

IMemStream::IMemStream(const char* base, size_t size) : IMemBuf(base,size), std::istream(static_cast<std::streambuf*>(this))
{
}

The exact error message is the following:

'IMemStream::IMemStream(const IMemStream &)': attempting to reference a deleted function

  • 2
    `istream`s cannot be copied. `IMemStream` [isa](https://en.wikipedia.org/wiki/Is-a) `istream`, so it cannot be copied either. Somewhere you are accidentally copying one and if you read more of the error messages, the compiler may tell you exactly where. – user4581301 Oct 30 '19 at 03:00
  • Unfortunately in the Visual Studio compiler this is the only error/warning/normal message I see. I'm not sure exactly what you mean by copying. Do you mean copying somewhere in the __istream__ constructor? Or do you mean somewhere in the the file instantiating the __IMemStream__ object? – joshuarobinsonson Oct 30 '19 at 16:59
  • I can't help you much here due to the lack of information provided. All I can say for sure is `IMemStream::IMemStream(const IMemStream &)` is a copy constructor, and if the code needs a copy constructor somewhere a `IMemStream` must get copied. See if the full build output in the Output tab (down near the Error List) contains more information. The Error List's purpose is to give a quick overview and often leaves out important information – user4581301 Oct 30 '19 at 17:07
  • Consider backing up your code and then carving it down to a [mcve]. – user4581301 Oct 30 '19 at 17:08
  • Ah, I see. I think the issue was in the source file where I was instantiating the class incorrectly. Essentially I had `IMemStream bStream = IMemStream(b, size)`. But it should have been `IMemStream bStream(b, size)` instead. I'm a bit new to C++ so that whole copy constructor stuff is new to me. Thanks! – joshuarobinsonson Oct 30 '19 at 17:38
  • Also I don't think have enough reputation to up vote you... – joshuarobinsonson Oct 30 '19 at 17:43
  • 1
    No worries. I didn't make a formal answer because while I could tell you what was likely wrong, I couldn't prove it or offer a solution. No one wants or needs an answer that's just , "This is what you got wrong, plebe." – user4581301 Oct 30 '19 at 17:53

0 Answers0