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

发布于 2024-11-09 05:33:25 字数 4006 浏览 6 评论 0原文

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 存在,因为我认为它可以修复我从虚拟中得到的“ostream”或“istream”“尚未声明”错误; Print、Read、Write、DRead、DWrite、ReadHeader 和 WriteHeader 函数原型。这些是该文件中唯一的错误。 .cpp 文件中的错误有些相关,我得到相同的“istream”和“ostream 尚未声明”错误。非常感谢任何帮助,如果需要更多详细信息,请告诉我。

-马凯尔 更新,查尔斯沃思爵士的建议成倍地减少了错误。在 WriteHeader 的虚拟函数原型的头文件中,生成“candidate is: virtual int IOBuffer::WriteHeader(std::ostream)”错误。 其余 5 个错误位于 .cpp 文件中,其中三个来自 DRead 的定义(每行一个)。第一行说

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

On a side note 为什么格式如此陌生?我在 cplusplus.com 此处查找了 ostream ,我想这可能是因为我使用整数作为我的搜索偏移量。继续,下面一行说

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

The return statements 说了一些非常奇怪的事情,

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 错误。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事与诗 2024-11-16 05:33:25

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

不太推荐的方法是声明 using namespace std;,这会将整个 std 命名空间拉入您当前所在的任何范围(以省去编写 std 的麻烦) code>std:: 每次)。请注意,在头文件中使用 using namespace ... 声明被认为是极其不好的做法。这些应该仅为源文件保留。

更新

大多数新错误消息是因为您混淆了 istreamostream。例如,istream 有一个名为 seekg 的函数,而不是 seekp

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.

滿滿的愛 2024-11-16 05:33:25

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

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

您收到最后一个错误是因为 IOBuffer.cpp 使用 const 定义 — int IOBuffer::WriteHeader (ostream & stream) const — 但 IOBuffer.h 使用非常量声明 —virtual int WriteHeader (ostream *);。您需要选择其中之一(即它们要么都需要在末尾有 const ,要么都不需要)。

您对如何使用 const 感到困惑吗?您将写入函数声明为 const 并将读取函数声明为非 const 是否有任何特殊原因?通常情况恰恰相反...

有关更多信息,请查看这篇文章关于 const 正确性,特别是问题 “什么是const 成员函数?”

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?".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文