Dragonfly Q&A

Getting an error " Cannot convert 'tg::Manager' to 'tg::LogManager &' "

0 like 0 dislike

Currently my main code consists of one line:


tg::LogManager &log_manager = tg::LogManager::getInstance();


This line is meant to get the instance of the log manager, but I'm receiving an error (C2440) that claims it cannot convert 'tg::Manager' to 'tg::LogManager &'. My code for getInstance() exists in Manager.cpp and uses the singleton design pattern, the only difference being that I check if the manager has already run startUp(). The code is listed below:


tg::Manager tg::Manager::getInstance() {
 
  static Manager manager;
 
  if (!is_started) {
    manager.startUp();
  }
 
  return manager;

}


is_started is a static boolean that determines if a manager has started or not. In the case of LogManager, the startUp() method sets its type, opens the log file, and sets is_started to true.

Anyone have any tips for this?

 

asked Sep 9, 2016 in Engine Development by Thomas-Lourenco

1 Answer

0 like 0 dislike
}You don't actually instantiate a base Manager class.  Instead, in LogManager startup() (and all derived managers), you invoke startup in the parent Manager class.  So:

<pre>

int LogManager::startUp() {

    ...
    Manager::startUp();

    ...

}
</pre>

You don't actually need to define getInstance() for Manager.  However, you do for Logmanager.  Something like what you have above for Manager, but for the LogManager.
answered Sep 9, 2016 by claypool

I'm still left very confused by what's occurring here. I know that you never create a generic manager, but I'm trying to create a log manager and get its instance.

Should my code look more like this then?

int Manager::startUp() {

  • new Manager();
  • Sets is_started to true

}

 Then in LogManager.cpp:

int LogManager::startUp() {

  • Manager::startUp();
  • Set the type of manager to "log_manager"
  • Open the file

Doing something like this seems to give me the exact same error.

You never create a base Manager.  So, you never do anything like "new Manager()" or "Manager manager;"

Setting is_started to true in Manager::startUp() is right.

Make sense?

It still leaves me confused because the Singleton design pattern in the slides shows the getInstance() method as having two simple lines:


static Singleton single;

return single;


So in this case it would seem like we want to apply the same technique to the getInstance() method in Manager as every manager could use the same getInstance(); method.

Even if it made sense to instantiate a Manager base object, doing so would not be a substitute for instantiating a derived class.   Think of our gun + shotgun example from class.  If you make a new gun() that is not the same as a new shotgun().  They are different objects.  Likewise, a new Manager is not the same as a new LogManager.  So, each derived class still needs to be instantiated separately - it needs it's own getInsatnce() method.
More information about Dragonfly can be found on the Dragonfly Home Page.
...