c++classcontainers

How to use container class in Item class, if it is has not been defined yet


I have an item class CItem and the container class CListOfTables, which included subordinated container class CTable (and this one includes items). For some function Aktualizuj() (function which changed name of item) I need to hand over a reference to subordinated class. But when I try to declare function in class CItem, I don't know class neither CListOfTables nor CTable. I could declare class CListOfTables:

class CListOfTables;

but no CListOfTables::CTable:

class CListOfTables::CTable; //makes error

Is some clever solution for this?

CODE:

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

Solution

  • You've gotten yourself tangled up but good. class CListOfTables::CTable; can't work because at the point you need it, CListOfTables hasn't been defined, so you can't get at its members, and that includes CTable.

    But because CListOfTables only needs a reference to CItem, you can forward declare CItem, then define CListOfTables and after that, define CItem.

    Example:

    class CItem; // we can forward declare this
    class CListOfTables
    {
    
    public:
        //Container of CItem
        class CTable : public set<CItem*> // so the pointer needed here is satisfied
        {
    
    
        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
    
    
    // and now everything is in place for  the definition of CItem
    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);
    
    };
    

    This will still fail to compile, but for reasons, converting too loosely between char and wide chars for one, completely unrelated to the question.