0

I have been working on a simple header only library, but I've run into a cyclic issue I can't figure. I'm returning by type which prob doesn't help matters.

template<typename T>
class Foo;

class Bar;


class Baz
{
public:

   template<typename T>
   T getAFoo<T>()
   {
      return Foo<T>();
   }


   Bar getABar() // Prefer return by type in this situation.
   {
      return Bar(); // Error.
   }
};

template<typename T>
class Foo {
   /* ... */
   void doSomething() { m_baz.something(); }
   Baz &m_baz;
};

class Bar {
   /* ... */
   void doSomething() { m_baz.something(); }
   Baz &m_baz;
};

I tried to prototype the function Bar getABar() then defining it after Bars definition but that just spewed out duplicated symbol errors.

Kinda feel that I just need it to be a template with no value.

  • You should consider this thread http://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration . – ciechowoj Aug 25 '14 at 20:41

1 Answers1

0

I'm sorry too.How about this:

template<typename T>
class Foo;

class Bar;

class Baz
{
public:

    template<typename T>
    T getAFoo()
    {
        return Foo<T>();
    }

    void something(){}

    inline Bar getABar();
};

template<typename T>
class Foo {
    /* ... */
    void doSomething() { m_baz.something(); }
    Baz &m_baz;
};

class Bar {
public:
    Bar(Baz& baz)
        :m_baz(baz)
    {
    }
    /* ... */
    void doSomething() { m_baz.something(); }
    Baz &m_baz;
};

Bar Baz::getABar()
{
    return Bar(*this);
}
wuqiang
  • 634
  • 3
  • 8
  • Ah sorry I should have mentioned that Foo and Bar need to use Baz. so need more than just their size. I've updated to reflect that in the question. –  Aug 26 '14 at 05:52
  • @PhilCK,I have modified it. – wuqiang Aug 26 '14 at 10:57