1

私は次のようなコンテナクラスに取り組んでいます:

class hexFile {
public:
    HANDLE theFile;
    unsigned __int64 fileLength;
    hexFile(const std::wstring& fileName)
    {
        theFile = CreateFile(fileName.c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
        if (theFile == INVALID_HANDLE_VALUE);
        {
            throw std::runtime_error(eAsciiMsg("Could not open file!"));
        }
        BY_HANDLE_FILE_INFORMATION sizeFinder;
        GetFileInformationByHandle(theFile, &sizeFinder);
        fileLength = sizeFinder.nFileSizeHigh;
        fileLength <<= 32;
        fileLength += sizeFinder.nFileSizeLow;
    };
    ~hexFile()
    {
        CloseHandle(theFile);
    };
    hexIterator begin()
    {
        hexIterator theIterator(this, true);
        return theIterator;
    };
    hexIterator end()
    {
        hexIterator theIterator(this, false);
        return theIterator;
    };
};

一致する反復子クラスは次のようになります。

class hexIterator : public std::iterator<std::bidirectional_iterator_tag, wchar_t>
{
    hexFile *parent;
public:
    bool highCharacter;
    __int64 filePosition;
    hexIterator(hexFile* file, bool begin);
    hexIterator(const hexIterator& toCopy);
    ~hexIterator();
    hexIterator& operator++()
    {
        return ++this;
    }
    hexIterator& operator++(hexIterator& toPlus);
    hexIterator& operator--()
    {
        return --this;
    }
    hexIterator& operator--(hexIterator& toMinus);
    hexIterator& operator=(const hexIterator& toCopy);
    bool operator==(const hexIterator& toCompare) const;
    bool operator!=(const hexIterator& toCompare) const;
    wchar_t& operator*();
    wchar_t* operator->();
};

私の問題は...両方のクラスを他の観点から実装する必要があることです。たとえば、イテレーターが定義されているとき、コンテナーはまだ定義されていないため、イテレーター内でコンテナーを参照する方法がわかりません。

どうすればこれを達成できますか?

ビリー3

4

3 に答える 3

5

順方向に一方を他方の前に宣言します。別のクラスの宣言で前方宣言されたクラスへの参照を使用できるため、これは機能するはずです。

class hexFile; // forward

class hexIterator : ,,, {
   ...
};

class hexFile {
   ...
};
于 2009-06-18T21:31:48.870 に答える
3

.h前方参照でファイルを開始します。

class hexFile;

次に、 (へのポインターclass hexIteratorのみが必要なためコンパイルされます) の完全な定義を続けます。次に、 (コンパイラーは に関するすべてを知っているため、これで問題なくコンパイルされます) の完全な定義を続けます。hexFileclass hexFilehexIterator

.cppファイルにはもちろんを含めているため、.hすべてが既知であり、メソッドを任意の順序で実装できます。

于 2009-06-18T21:32:37.120 に答える
2

クラスの宣言から定義を分離することをお勧めします。ヘッダー ファイルで、hexFile クラスを前方宣言し、ヘッダー ファイルで両方を完全に宣言します。その後、関連付けられたソース ファイルで個々のクラスをより詳細に定義できます。

于 2009-06-18T21:33:26.730 に答える