How to pass structures to templates class functions

Asked

Viewed 62 times

0

I just don’t understand why it’s wrong. This code works.

template
class test{
public:
    struct st1{
        T a, b, c;
    };
    struct st2{
        T d, e, f;
    };

    T foo1(st1 *st);
    st1 *foo2();
};

template
typename test::st1 *foo2()
{
    return 0;
}

template
T test::foo1(typename st1 *st)
{
    return 0;
}

But this one doesn’t work.

template
class test{
public:
    struct st1{
        T a, b, c;
    };
    struct st2{
        T d, e, f;
    };

    T foo1(st1 *st);
    st1 *foo2();
    st1 *foo3(st2 *st);
};

template
T test::foo1(typename st1 *st)
{
    return 0;
}

template
typename test::st1 *foo2()
{
    return 0;
}

template
typename test::st1 *foo3(typename st1 *st)
{
    return 0;
}
  • Honestly, I don’t believe that anyone works http://coliru.stacked-crooked.com/a/e6f47cd72c94782b

1 answer

0

The syntax of your second code is completely wrong.

template <typename T>
class test{
public:
    struct st1{
        T a, b, c;
    };
    struct st2{
        T d, e, f;
    };

    T foo1(st1 *st);
    st1 *foo2();
    st1 *foo3(st2 *st);
};

template <typename T>T test<T>::foo1(st1 *st)
{
    return 0;
}

template <typename T> typename test<T>::st1* test<T>::foo2()
{
    return 0;
}

template <typename T> typename test<T>::st1* test<T>::foo3(st2 *st)
{
    return 0;
}

This is the correct syntax, but I would like to know what exactly you hope to achieve with this class. You are studying templates?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.