views:

168

answers:

3

Hello :)

I'm working on an container class that looks something like this:

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

And an iterator class to match that looks like this:

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

My problem is... both classes need to implemented in terms of the other. I'm unsure how to reference the container inside the iterator, for example, because when the iterator is defined, the container hasn't been defined yet.

How might one accomplish this?

Billy3

+5  A: 

Forward declare one before the other. You can use references to a forward declared class in the declaration of another class, so this should work:

class hexFile; // forward

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

class hexFile {
   ...
};
sth
Upvoted for a correct answer, checkmarked for the fastest one ;)
Billy ONeal
you know, it's funny. A long while ago, I had a problem similar to this, and nobody told me I could forward declare a class... I had to like, make some other class, with a pure virtual function, and then derive both of the other classes from it, and use polymorphism to pass pointers of eachother around... it was a mess.
Carson Myers
+3  A: 

Start your .h file with a forward reference:

class hexFile;

then follow with the full definition of class hexIterator (which will compile because it only needs a pointer to hexFile), then the full deifnition of class hexFile (which will compile just fine now because by then the compiler knows everything about hexIterator).

In the .cpp file, since you include the .h of course, everything will be known and you can implement the methods in any order you wish.

Alex Martelli
+2  A: 

I would recommend separating the definition from the declaration of the classes. In the header file, forward declare the hexFile classes and then fully declare both of them in the header file. You can then define the individual classes in more detail in the associated source file.

Joe Corkery