函数和类错误

发布于 2024-11-09 10:18:44 字数 2692 浏览 0 评论 0原文

class BufferFile{  
public:  
        BufferFile(IOBuffer &);  
        int Open(char *);  
        int Create(char *);  
        int Close();  
        int Rewind();  
        int Read(int recaddr = -1);  
        int Write(int recaddr = -1);  
        int Append();  
        IOBuffer & GetBuffer();  
    protected:  
        IOBuffer & Buffer;  
        std::fstream File;  
        int HeaderSize;  
        int ReadHeader();  
        int WriteHeader();  
    };    

    BufferFile::BufferFile(IOBuffer & from):Buffer(from){}    

    int BufferFile::Read(int recaddr){  
        if(recaddr==1) return Buffer.Write(File);  
        else return Buffer.DWrite(File, recaddr);  
    }  

    int BufferFile::Append(){  
        File.seekp(0,std::ios::end);  
        return Buffer.Write(File);  
    }  

    IOBuffer & BufferFile::GetBuffer(){  
        return Buffer;  
    }  

    int BufferFile::ReadHeader(){  
        return Buffer.ReadHeader(File);  
    }  

    int BufferFile::WriteHeader(){  
        return Buffer.WriteHeader(File);  
    }  

我从 IOBuffer 字段收到几个错误,说它没有在函数作用域中声明,或者在构造函数上的“&”标记之前“预期为‘)’,是什么导致了这些错误?

以下是该项目涉及到的所有文件: Person.h!
Buffile.cpp
BuffFile.h
Delim.cpp
Delim.h
Fixfld.cpp
Fixfld.h
FixLen.cpp
FixLen.h
Iobuffer.cpp
Iobuffer.h
Length.cpp 长度.h
Varlen.cpp
Varlen.h

class BufferFile{  
public:  
        BufferFile(IOBuffer &);  
        int Open(char *);  
        int Create(char *);  
        int Close();  
        int Rewind();  
        int Read(int recaddr = -1);  
        int Write(int recaddr = -1);  
        int Append();  
        IOBuffer & GetBuffer();  
    protected:  
        IOBuffer & Buffer;  
        std::fstream File;  
        int HeaderSize;  
        int ReadHeader();  
        int WriteHeader();  
    };    

    BufferFile::BufferFile(IOBuffer & from):Buffer(from){}    

    int BufferFile::Read(int recaddr){  
        if(recaddr==1) return Buffer.Write(File);  
        else return Buffer.DWrite(File, recaddr);  
    }  

    int BufferFile::Append(){  
        File.seekp(0,std::ios::end);  
        return Buffer.Write(File);  
    }  

    IOBuffer & BufferFile::GetBuffer(){  
        return Buffer;  
    }  

    int BufferFile::ReadHeader(){  
        return Buffer.ReadHeader(File);  
    }  

    int BufferFile::WriteHeader(){  
        return Buffer.WriteHeader(File);  
    }  

I am getting several errors form the IOBuffer field, saying that it was not declared in the function scopes or "expected `)' before ‘&’ token" on the constructor, what is causing these?

Here are all the files involved in this project:
Person.h!
Buffile.cpp
BuffFile.h
Delim.cpp
Delim.h
Fixfld.cpp
Fixfld.h
FixLen.cpp
FixLen.h
Iobuffer.cpp
Iobuffer.h
Length.cpp
Length.h
Varlen.cpp
Varlen.h

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

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

发布评论

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

评论(1

娇俏 2024-11-16 10:18:44

我认为你的问题是这样的(来自 Buffile.h):

#ifndef IOBUFFER
#define IOBUFFER
#include "Iobuffer.h"
#endif

...该逻辑破坏了 Iobuffer.h 中的类似/冗余逻辑:

#ifndef IOBUFFER
#define IOBUFFER
class IOBuffer{
[...]
#endif

问题是 Iobuffer.h 中的“class IOBuffer”声明永远不会被解析,因为编译器值 IOBUFFER 已在 Buffile.h 中定义,因此 Iobuffer.h 顶部的 #ifndef IOBUFFER 未激活。

正确的方法是修改 Buffile.h 以仅包含 #include "Iobuffer.h" 行,并将其留给 Iobuffer.h 的内容来执行 #ifndef 和 #define 操作。

I think your problem is this (from Buffile.h):

#ifndef IOBUFFER
#define IOBUFFER
#include "Iobuffer.h"
#endif

... that logic breaks the similar/redundant logic that you have in Iobuffer.h:

#ifndef IOBUFFER
#define IOBUFFER
class IOBuffer{
[...]
#endif

The problem is that the declaration of "class IOBuffer" in Iobuffer.h is never parsed, because the compiler value IOBUFFER was already defined inside Buffile.h, and thus the #ifndef IOBUFFER at the top of Iobuffer.h is not activated.

The right way to do it is to modify Buffile.h to include only the #include "Iobuffer.h" line, and leave it up to the contents of Iobuffer.h to do the #ifndef and #define stuff.

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