VS 2008 SP1 C++ 项目,但 friend class
VS 2008 SP1
C++
friend class
我做错了什么 friend
friend
// EncryptionTypes.h file #pragma once //#include "Encryption.h" //adding this line doesn't help using namespace crypto; struct FILE_DATA_CACHE{ FILE_DATA_CACHE() { }; ~FILE_DATA_CACHE() { } friend class CEncryption; private: bool _isIndexFileUsed() { return bResult; } };
然后:
// Encryption.h #pragma once #include "EncryptionTypes.h" namespace crypto { class CEncryption { public: CEncryption(void); ~CEncryption(void); private: BOOL _openFile(); private: FILE_DATA_CACHE gFData; }; };
最后一点:
// Encryption.cpp #include "StdAfx.h" #include "Encryption.h" namespace crypto { CEncryption::CEncryption(void) { } CEncryption::~CEncryption(void) { } void CEncryption::_openFile() { //The line below generates this error: //1>.\Encryption.cpp(176) : error C2248: 'FILE_DATA_CACHE::_isIndexFileUsed' : cannot access private member declared in class 'FILE_DATA_CACHE' //1> c:\users\blah-blah\EncryptionTypes.h(621) : see declaration of 'FILE_DATA_CACHE::_isIndexFileUsed' //1> c:\users\blah-blah\EncryptionTypes.h(544) : see declaration of 'FILE_DATA_CACHE' gFData._isIndexFileUsed(); } };
您有一个循环依赖性问题。
FILE_DATA_CACHE ,这在EncryptionTypes.h中定义。 加密类型。h需要 CEncryption
FILE_DATA_CACHE
CEncryption
幸运的是,您可以使用 在EncryptionType.h中。
// EncryptionTypes.h file #pragma once // Can't #include Encryption.h. That will lead to circular // #includes. namespace crypto { // Forward declaration of crypto::CEncryption class CEncryption; } struct FILE_DATA_CACHE{ FILE_DATA_CACHE() { }; ~FILE_DATA_CACHE() { } friend class crypto::CEncryption; private: bool _isIndexFileUsed() { return bResult; } };