Iobuffer.cpp
#include "Iobuffer.h"
IOBuffer::IOBuffer (int maxBytes){
Init (maxBytes);
}
IOBuffer & IOBuffer :: operator = (const IOBuffer & buffer){
if(MaxBytes< buffer.BufferSize) return *this;//fail
Initialized = buffer.Initialized;
BufferSize = buffer.BufferSize;
memcpy(Buffer, buffer.Buffer, buffer.BufferSize);
NextByte = buffer.NextByte;
Packing = Packing;
return *this;
}
void IOBuffer::Clear(){
NextByte = 0;
Packing = true;
}
void IOBuffer::Print(ostream & stream) const{
stream<<"MaxBytes "<<MaxBytes<<" BufferSize "<<BufferSize;
}
int IOBuffer::Init (int maxBytes){
Initialized = false;
if (maxBytes < 0) maxBytes = 0;
MaxBytes = maxBytes;
Buffer = new char[MaxBytes];
BufferSize = 0;
Clear ();
return 1;
}
int IOBuffer::DRead(istream & stream, int recref){
stream.seekp(recref, ios::beg);
if(stream.tellp() != recref) return -1;
return Write(stream);
}
static const char * headerStr = "IOBuffer";
static const int headerSize = strlen(headerStr);
int IOBuffer::ReadHeader(istream & stream){
char str[9];
stream.seekg(0, ios::beg);
stream.read(str, headerSize);
if(!stream.good()) return -1;
if(strncmp(str,headerStr, headerSize)==0) return headerSize;
else return -1;
}
int IOBuffer::WriteHeader (ostream & stream) const{
stream.seekp(0, ios::beg);
stream.write(headerStr, headerSize);
if(!stream.good()) return -1;
return headerSize;
}
its accompanied Iobuffer.h
#include <cstring>
#include <iostream>
class IOBuffer{
public:
IOBuffer (int maxBytes = 1000);
IOBuffer & operator = (const IOBuffer &);
virtual void Clear ();
virtual int Pack (const void * field, int size = -1) = 0;
virtual int Unpack (void * field, int maxbytes = -1) = 0;
virtual void Print(ostream &) const;
int Init (int maxBytes);
virtual int Read (istream & x) = 0;
virtual int Write (ostream & x) const = 0;
virtual int DRead(istream &, int recref);
virtual int DWrite(ostream &, int recref) const;
virtual int ReadHeader (istream &);
virtual int WriteHeader (ostream *);
protected:
int Initialized;
char * Buffer;
int BufferSize;
int MaxBytes;
int NextByte;
int Packing;
};
This is an assignment from my File Systems course. In Iobuffer.h, #include <iostream> is there because I supposed it would fix the “ostream” or “istream” “has not been declared” errors I am getting from the virtual; Print, Read, Write, DRead, DWrite, ReadHeader, and WriteHeader function prototypes. Those are the only errors from that file. The errors in the .cpp file correlate somewhat, I get the same “istream” and “ostream have not been declared” errors. Any help is much appreciate, let me know if further detail is needed.
-Macaire
Update, Sir Charlesworth’s suggestion cut down the errors exponentially. In the header file for WriteHeader’s virtual function prototype “candidate is: virtual int IOBuffer::WriteHeader(std::ostream)” error is generated.
The remaining 5 errors are in the .cpp file, three of them from DRead’s definition(one from each line). The first line says
‘struct std::basic_istream<char, std::char_traits<char> >’ has no member named ‘seekp’
On a side note why is that formatting so foreign? I looked up ostream here at cplusplus.com, and I suppose it could be because I am using an integer as my seek offset. Continuing, the following line says
‘struct std::basic_istream<char, std::char_traits<char> >’ has no member named ‘tellp’
The return statement says something that is very curious,
no matching function for call to ‘IOBuffer::Write(std::basic_istream<char, std::char_traits<char> >&)’
The final error is prototype for
‘int IOBuffer::WriteHeader(std::ostream&) const’ does not match any in class ‘IOBuffer’
and yes that was 5 not 6 error.
Most names in the standard library live within the namespace
std. So common practice is simply to fully qualify them when you use them (std::ostreaminstead ofostream, and so on).A less recommended approach is to declare
using namespace std;, which will pull the entirestdnamespace into whatever scope you’re currently in (to save you the trouble of writingstd::every time). Note that it is considered extremely bad practice to haveusing namespace ...declarations in header files. These should be reserved for source files only.UPDATE
The majority of your new error messages are because you’ve confused
istreamandostream.istreamhas a function calledseekg, notseekp, for instance.