0

I'm trying to get the following to compile. However whatever various configurations I try, the compiler doesn't like the fact that I've got a struct defined inside the derived struct that is being referenced in the base struct. Is there any way to resolve this without moving the item struct outside of the derived struct?

#include <vector>

template <typename T>
struct BaseTask
{
    int id;
    std::vector<typename T::Item> items;       // <---- invalid use of incomplete type 'struct DerivedTask'

    typename T::Item* GetItemById(int id)      // <---- invalid use of incomplete type 'struct DerivedTask'
    {
        for (auto item : items)
        {
            if (item.id == id)
            {
                return &item;
            }
        }
        return nullptr;
    }
};

struct DerivedTask : public BaseTask<DerivedTask>
{
    struct Item
    {
        int id;
    };

    int specialDerivedValue;
};

I've tried forward declaring both the derived struct and the substruct. I've tried forward declaring BaseTask and then defining it at the bottom of the file. Thanks for taking the time to read.

  • 1
    If you want to look inside `T`, it should be a complete type, there is no way to bypass this requirement. If you want to hide `Item`, one solution might be to put it into `DerivedTaskBase` and inherit `DerivedTask` from `DerivedTaskBase`. Then `DerivedTaskBase` could be a complete type inside `BaseTask`. – Evg Jun 27 '23 at 16:14
  • See https://stackoverflow.com/questions/35428422/crtp-accessing-incomplete-type-members – user1806566 Jun 27 '23 at 17:19

0 Answers0