0

I have a program where two classes need each other in their Initialization process, but so far all I get is Compilation Error and I have no idea how to make it work

in Class MainComponent

class MainComponent  : {
   private:
      PlaylistComponent playlistComponent{formatManager, *this};
}

in Class PlaylistComponent

class PlaylistComponent :{
   private:
      MainComponent& mainComponent;
}
PlaylistComponent::PlaylistComponent(juce::AudioFormatManager &_formatManager, MainComponent &_mainComponent):
formatManager(_formatManager),
mainComponent(_mainComponent){
   //something else
}

and here are the error messages:

error: ‘MainComponent’ has not been declared
   29 |     PlaylistComponent(juce::AudioFormatManager&, MainComponent&);
error: ‘MainComponent’ does not name a type; did you mean ‘Component’?
   74 |     MainComponent& mainComponent;
error: ‘PlaylistComponent’ does not name a type; did you mean ‘MainComponent’?
   49 |     PlaylistComponent playlistComponent{formatManager, *this};
error: ‘MainComponent’ has not been declared
   29 |     PlaylistComponent(juce::AudioFormatManager&, MainComponent&);

error: could not convert ‘{((MainComponent*)this)->MainComponent::formatManager, (*(MainComponent*)this)}’ from ‘<brace-enclosed initializer list>’ to ‘PlaylistComponent’
   49 |     PlaylistComponent playlistComponent{formatManager, *this};
      |                                                             ^
      |                                                             |
      |                                                             <brace-enclosed initializer list>

So far I have tried different approach such as " forward declarations"(Two classes that refer to each other) but it did not work, and the error message was that playlistComponent has incomplete type PlaylistComponent

I know that it's because one Class needs to be initialized before the other, but how could I do this when both Classes need each other during the Initialization process?

Mattmmmmm
  • 155
  • 1
  • 6
  • Show your failed attempt. Make sure it doesn't have any typos (like the code you have now). – HolyBlackCat Feb 12 '22 at 08:20
  • 2
    `PlaylistComponent` needs to have its definition available before `MainComponent`, while `MainComponent` only requires a forward declaration for `PlaylistComponent` (since it only declares a reference of the latter). Please make sure your include directives are set up accordingly. – Mike Feb 12 '22 at 08:39
  • 3
    Here is a compilable example that shows what I mean: https://wandbox.org/permlink/sXXf35yERBxOx2Gu. – Mike Feb 12 '22 at 08:40
  • 1
    Yes, @Mike is right, add `class PlaylistComponent;` forward declaration before you declare `MainComponent`. – Alexey Ivanov Feb 12 '22 at 08:46

1 Answers1

1

You need something like this(forward declaration: because you didn't show us how you used forward decleration I show that again here):

//in  A.h file
class B;
class A{
    B* b; // or anything else that dont need definition here and just need definition in cpp file.
}
//in  B.h file
class A;
class B{
    A* a; // or anything else that dont need definition here and just need definition in cpp file.
}
// in A.cpp 
include "A.h"
include "B.h"
// in B.cpp 
include "B.h"
include "A.h"

Note: always write the namespaces too if the class has namespace(in forward decleration) for example:

namespace mine{
class A;
}
H.M
  • 425
  • 2
  • 16