0

I have two classes, Graph and Frame, that cross reference each other.

This is due to need of information on what graphs does one frame own and get the owning frame from graph.

So when frame are updated with a new graph, the added graph has to call its old owner frame to release it before being added by the new.

I have declared the class Frame before the graph but it won't work.

class Frame;
class Graph;

// definition of Graph
class Graph
{
private:
    Frame* Owner;

public:
    
    Graph(const Graph& copy): Owner(copy.GetOwner()) {}

    bool UpdateOwner(Frame& owner)
    {
        // release it from previous owner
        if (Owner.IsValid())
            Owner.release_graph(this);
        
        // insert it into new owner
        owner.add_graph(this);
        Owner = owner;

        return;
    }

    BluePrint_Frame* GetOwner() const
    {
        return Owner;
    }

};

// and the definition of Frame
class Frame
{
vector<Graph> graphs;
    void release_graph(Graph& graph)
    {
        // find graph and erase it from list
        ...
        return ERROR;
    }   
    void add_graph(Graph& graph)
    {
        graphs.push_back(graph);
        return ERROR;
    }

}


When compiling the code gives the error that Frame is undefined, due to class function of Frame IsValid(), release_graph, and add_graph.

  • As with the forward declaration of the class, you can tell the compiler a member function "is there" in the definition of the class without defining the function inline. In general you do not put the function definitions in the headers, but move them to a .cpp file corresponding to the header; you can however simply leave the declaration and move the function definition to the end, if you need inline functions: `class Graph {... bool UpdateOwner(Frame& owner); ... }; class Frame {...}; inline bool Graph::UpdateOwner(Frame& owner) { ... }` (remove the `inline`, if you move f def to `Graph.cpp` – fabian Mar 06 '22 at 09:44

0 Answers0