开发者

C++ stream, function declaration, and other questions

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::ostream instead of ostream, and so on).

A less recommended approach is to declare using namespace std;, which will pull the entire std namespace into whatever scope you're currently in (to save you the trouble of writing std:: every time). Note that it is considered extremely bad practice to have using 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 istream and ostream. istream has a function called seekg, not seekp, for instance.


Your last two errors are const problems.

You're getting the second-to-last error because you're calling Write, which is a const function, from DRead, which is a non-const function. You could probably remove const from the declaration for Write, but make sure that you do the same in all classes that derive from it, too!

You're getting the last error because IOBuffer.cpp uses a const definition—int IOBuffer::WriteHeader (ostream & stream) const—but IOBuffer.h uses a non-const declaration—virtual int WriteHeader (ostream *);. You need to choose one or the other (i.e. they either both need to have const at the end or neither).

Are you confused about how to use const? Is there any particular reason you're declaring your writing functions as const and your reading functions as non-const? Usually it's the other way around...

For more information, check out this article on const-correctness and in particular the question "What is a const member function?".

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜