marcus said:
[...]
Can you be more specific about S beeing a type and how I am to do a
S.func1() call. I use the class keyword in "template <class S>", so
new to this as I am I expected S to be a class and not a type.
So my question here is not really about missing semicolons or such,
but about how to use the methods of a class that is beeing used in a
template class/method (see my attempt S.func1()). I want to be able to
do a call like this, so how do I do it?
Here is your problem:
---------------------------------------
template<class S> struct A
{
void foo();
};
struct HasFunc1
{
void func1();
};
template<class S> void A<S>::foo()
{
S.func1(); /// ???????????????????????
}
int main()
{
A<HasFunc1> a;
a.foo();
}
---------------------------------------
I don't know the solution to your problem. But the reason this is not
correct code is simple: non-static member functions (like func1 here)
_require_an_instance_of_their_class_ in order to be called. The example
of that is in the 'main' function: to call 'foo' you need the 'a'.
So, in the A<S>::foo, if you want to call 'func1', you need _an_instance_
of the 'S' class. One possible solution is for 'foo' to have an argument
of type S (or S&), like this:
template<class S> void A<S>::foo(S& rs)
{
rs.func1();
}
Another possible solution is to have a member of class S in A and use it
to call 'func1'. There are probably variations on the theme, and which
one is right depends on your design, if you have it.
One thing I need to mention. You may have noticed that in my explanation
of the reason I did say "non-static". So, another _possible_ solution is
to assume that 'func1' is a static member of S. Then the syntax to call
it becomes slightly different:
S::func1();
but other static member limitations apply. Again, which one is the real
solution to your problem is impossible for me to divine.
V