我有一个项目类
CItem
和容器类 CListOfTables
,其中包括从属容器类 CTable
(这一个包括项目)。对于某些函数Aktualizuj()
(更改项目名称的函数),我需要移交对从属类的引用。
但是当我尝试在类 CItem 中声明函数时,我既不知道类 CListOfTables
也不知道 CTable
。
我可以声明class CListOfTables
:
class CListOfTables;
但是没有
CListOfTables::CTable
:
class CListOfTables::CTable; //makes error
有什么聪明的解决方案吗?
代码:
// C++ program to read from a file
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include "set"
#include <Windows.h>
using namespace std;
struct CItem
{
protected:
wstring m_wsText;
public:
//Konstruktory
protected:
CItem(LPCTSTR lsText) : m_wsText(lsText ? lsText : L"") {}
CItem() = default; //emty konstruktor
public:
//Vrátí seznam tras v mapě jako řetězec názvů tras, oddělěný čárkami
LPCTSTR GetText() const { return m_wsText.data(); }
void SetText(LPCTSTR lsTxt) { m_wsText = lsTxt; }
size_t GetTextSize() const { return m_wsText.size(); }
//Here I need send CListOfTables::CTable to function because of I need actualize an Item in std:set,
// but CListOfTables::CTable has not yet been defined
inline bool Aktualizuj(CListOfTables::CTable* tbl, CItem* pCopy);
};
//Conteiner class o CTable
class CListOfTables
{
public:
//Container of CItem
class CTable : public set<CItem*>
{
protected:
CTable() = delete;
CTable(const CTable& druhy) = delete; //kopírovací konstruktor
void operator=(const CTable& kopie) = delete; //operátor přirazení
}; // CTable
// Atributes of CListOfTables
protected:
CTable m_sezOfTables[3];
public:
CTable& GetTbl(int iType)
{
return m_sezOfTables[iType];
}
}; //class CSezTblPopisu
int main()
{
}
inline bool CItem::Aktualizuj(CListOfTables::CTable* tbl, CItem* pCopy)
{
if (wcscmp(GetText(), pCopy->GetText()) == 0)
return;
auto it = tbl->find(this);
if (it != tbl->end())
{
CItem* item = *it;
tbl->erase(it);
item->SetText(pCopy->GetText());
tbl->insert(item);
return true;
}
else
return false;
}
你把自己搞乱了,但很好。
class CListOfTables::CTable;
无法工作,因为在您需要它时,CListOfTables
尚未定义,因此您无法获取其成员,其中包括 CTable
。
但是因为
CListOfTables
只需要对 CItem
的引用,因此您可以转发声明 CItem
,然后定义 CListOfTables
,然后定义 CItem
。
示例:
class CItem;
class CListOfTables
{
public:
//Container of CItem
class CTable : public set<CItem*>
{
protected:
CTable() = delete;
CTable(const CTable& druhy) = delete; //kopírovací konstruktor
void operator=(const CTable& kopie) = delete; //operátor přirazení
}; // CTable
// Atributes of CListOfTables
protected:
CTable m_sezOfTables[3];
public:
CTable& GetTbl(int iType)
{
return m_sezOfTables[iType];
}
}; //class CSezTblPopisu
struct CItem
{
protected:
wstring m_wsText;
public:
//Konstruktory
protected:
CItem(LPCTSTR lsText) : m_wsText(lsText ? lsText : L"") {}
CItem() = default; //emty konstruktor
public:
//Vrátí seznam tras v mapě jako řetězec názvů tras, oddělěný čárkami
LPCTSTR GetText() const { return m_wsText.data(); }
void SetText(LPCTSTR lsTxt) { m_wsText = lsTxt; }
size_t GetTextSize() const { return m_wsText.size(); }
//Here I need send CListOfTables::CTable to function because of I need actualize an Item in std:set,
// but CListOfTables::CTable has not yet been defined
inline bool Aktualizuj(CListOfTables::CTable* tbl, CItem* pCopy);
};
这仍然无法编译,但原因与问题完全无关。