类型“T”必须是可转换的,以便在泛型类型或方法中使用它作为参数“T”。

时间:2021-12-19 00:42:00

I have these two main classes. First the FSMSystem class:

我有这两个主要的类。第一个FSMSystem类:

public class FSMSystem<T> : MonoBehaviour where T : FSMSystem<T>
{
    private T m_Owner = default(T);

    protected FSMState<T> currentState;

    private Dictionary<int, FSMState<T>> m_states;

    public FSMSystem(T owner)
    {
        m_Owner = GameObject.FindObjectOfType(typeof(T)) as T; //owner;
        m_states = new Dictionary<int, FSMState<T>>();
    }

    protected void AddState( FSMState<T> state )
    {
        m_states.Add( state.GetStateID(), state );
    }
}

And the second class, FSMState:

第二类,FSMState:

public abstract class FSMState<T>
{   
    public abstract int GetStateID();

    public abstract void OnEnter (FSMSystem<T> fsm, FSMState<T> prevState);
    public abstract void OnUpdate (FSMSystem<T> fsm);
    public abstract void OnExit (FSMSystem<T> fsm, FSMState<T> nextState);
}

It leads to the following error:

它导致了以下错误:

error CS0309: The type 'T' must be convertible to 'FSMSystem<T>' in order to use it as parameter 'T' in the generic type or method 'FSMSystem<T>'

错误CS0309:类型'T'必须可转换为'FSMSystem ',以便在泛型类型或方法'FSMSystem '中使用它作为参数'T'

Can someone tell me how to resolve this? I see many other posts similar to this one but I'm not seeing the relationship.

有人能告诉我如何解决这个问题吗?我看到很多类似的帖子,但我没有看到这段关系。

2 个解决方案

#1


8  

The T of FSMState must also be constrained, otherwise it cannot be used as the T of FSMSystem - which has constraints placed on it (T : FSMSystem<T>).

FSMState的T也必须被约束,否则它不能被用作FSMSystem的T -它对它有约束(T: FSMSystem )。

If you would have provided the line number of the compiler error, I suspect it would point to the methods OnEnter, etc.

如果您提供了编译器错误的行号,我怀疑它会指向OnEnter的方法,等等。

#2


0  

It's big help to me. thank you all. I resolved the problem. I misunderstood about 'where' syntax.

这对我很有帮助。谢谢大家。我解决了问题。我误解了“where”的语法。

Here is the revised version that works.

这是修改后的版本。

public class FSMSystem<T> : MonoBehaviour where T : FSMSystem<T>
{
    private T m_Owner = default(T);

    protected FSMState<T> currentState;

    private Dictionary<int, FSMState<T>> m_states;

    public FSMSystem()
    {
        m_Owner = this as T;
        m_states = new Dictionary<int, FSMState<T>>();
    }

    protected void AddState( FSMState<T> state )
    {
        m_states.Add( state.GetStateID(), state );
    }
}


public abstract class FSMState<T> where T : FSMSystem<T>
{
    public abstract int GetStateID();

    public abstract void OnEnter (T fsm, FSMState<T> prevState);
    public abstract void OnUpdate (T fsm);
    public abstract void OnExit (T fsm, FSMState<T> nextState);
}

#1


8  

The T of FSMState must also be constrained, otherwise it cannot be used as the T of FSMSystem - which has constraints placed on it (T : FSMSystem<T>).

FSMState的T也必须被约束,否则它不能被用作FSMSystem的T -它对它有约束(T: FSMSystem )。

If you would have provided the line number of the compiler error, I suspect it would point to the methods OnEnter, etc.

如果您提供了编译器错误的行号,我怀疑它会指向OnEnter的方法,等等。

#2


0  

It's big help to me. thank you all. I resolved the problem. I misunderstood about 'where' syntax.

这对我很有帮助。谢谢大家。我解决了问题。我误解了“where”的语法。

Here is the revised version that works.

这是修改后的版本。

public class FSMSystem<T> : MonoBehaviour where T : FSMSystem<T>
{
    private T m_Owner = default(T);

    protected FSMState<T> currentState;

    private Dictionary<int, FSMState<T>> m_states;

    public FSMSystem()
    {
        m_Owner = this as T;
        m_states = new Dictionary<int, FSMState<T>>();
    }

    protected void AddState( FSMState<T> state )
    {
        m_states.Add( state.GetStateID(), state );
    }
}


public abstract class FSMState<T> where T : FSMSystem<T>
{
    public abstract int GetStateID();

    public abstract void OnEnter (T fsm, FSMState<T> prevState);
    public abstract void OnUpdate (T fsm);
    public abstract void OnExit (T fsm, FSMState<T> nextState);
}