C ++流,函数声明和其他问题

时间:2011-05-22 22:03:50

标签: c++ function stream

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;
}

其伴随的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;
    };

这是我的文件系统课程的作业。在Iobuffer.h中,#include <iostream>是因为我认为它会修复“ostream”或“istream”“尚未声明”我从虚拟中得到的错误;打印,读取,写入,DRead,DWrite,ReadHeader和WriteHeader函数原型。这些是该文件中唯一的错误。 .cpp文件中的错误有些相关,我得到相同的“istream”和“ostream尚未声明”错误。任何帮助都非常感谢,如果需要进一步的细节,请告诉我。

-Macaire 更新,查尔斯沃思爵士的建议以指数方式减少错误。在WriteHeader的虚函数原型“候选者是:virtual int IOBuffer :: WriteHeader(std :: ostream )”的头文件中生成错误。     其余5个错误在.cpp文件中,其中三个来自DRead的定义(每行一个)。第一行说

‘struct std::basic_istream<char, std::char_traits<char> >’ has no member named ‘seekp’

另外请注意为什么格式化如此陌生?我在cplusplus.com上查了ostream here,我想这可能是因为我使用整数作为我的搜索偏移量。继续,以下一行说

‘struct std::basic_istream<char, std::char_traits<char> >’ has no member named ‘tellp’

返回声明说的很奇怪,

no matching function for call to ‘IOBuffer::Write(std::basic_istream<char, std::char_traits<char> >&)’

最终错误是

的原型
‘int IOBuffer::WriteHeader(std::ostream&) const’ does not match any in class ‘IOBuffer’

并且是,这是5而不是6错误。

2 个答案:

答案 0 :(得分:6)

标准库中的大多数名称都位于名称空间std内。因此,通常的做法是在使用它们时完全限定它们(std::ostream而不是ostream,等等。)

不太推荐的方法是声明using namespace std;,这会将整个std命名空间拉到你当前所处的任何范围内(为了省去每次编写std::的麻烦)。请注意,在头文件中使用using namespace ...声明被认为是非常不良做法。这些应仅保留给源文件。

<强>更新

您的大多数新错误消息都是因为您混淆了istreamostreamistream有一个名为seekg的函数,而不是seekp

答案 1 :(得分:2)

您的最后两个错误是const个问题。

你得到倒数第二个错误,因为你从Write调用DRead这是一个const函数,这是一个非const函数。您可以从const的声明中删除Write,但请确保您在从它派生的所有类中执行相同操作!

您收到了最后一个错误,因为IOBuffer.cpp使用了const定义 - int IOBuffer::WriteHeader (ostream & stream) const - 但IOBuffer.h使用了非常量声明 - virtual int WriteHeader (ostream *);。你需要选择其中一个(即他们要么最后都需要const,要么两者都没有。)

您对如何使用const感到困惑吗?您是否有任何特殊原因要求将您的写作功能声明为const而您的阅读功能为非const?通常它是相反的......

有关详细信息,请查看this article on const-correctness,尤其是问题"What is a const member function?"