C ++ 컨테이너 / 반복자 종속성 문제
-
06-07-2019 - |
문제
나는 다음과 같은 모습을 보이는 컨테이너 클래스를 작업하고 있습니다.
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
해결책
앞으로 하나를 선언하십시오. 다른 클래스 선언에서 전달 선언 된 클래스에 대한 참조를 사용할 수 있으므로 다음과 같습니다.
class hexFile; // forward
class hexIterator : ,,, {
...
};
class hexFile {
...
};
다른 팁
당신의 시작 .h
전방 참조가있는 파일 :
class hexFile;
그런 다음 전체 정의를 따르십시오 class hexIterator
(a 만 있으면 컴파일됩니다 바늘 에게 hexFile
), 그런 다음의 완전한 탈선 class hexFile
(그때까지 컴파일러가 모든 것을 알고 있기 때문에 지금 잘 컴파일 할 것입니다. hexIterator
).
에서 .cpp
당신이 포함하므로 파일 .h
물론 모든 것이 알려져 있으며 원하는 순서로 방법을 구현할 수 있습니다.
클래스 선언에서 정의를 분리하는 것이 좋습니다. 헤더 파일에서 Forward는 Hexfile 클래스를 선언 한 다음 헤더 파일에 두 가지를 완전히 선언합니다. 그런 다음 관련 소스 파일에서 개별 클래스를 더 자세히 정의 할 수 있습니다.
제휴하지 않습니다 StackOverflow