Visual Studio says “Method must have a return type”

让人想犯罪 __ 提交于 2019-12-01 15:22:03

问题


It says

"Method must have a return type"

whenever I try to debug it.

I don't know how to fix this class

This is a player class for a c# coded 2d Game

public class player
{
    public float moveSpeed;
    public Vector2 position;
    public Texture2D texture;

    //default constructer
    public Player(Texture2D tex, Vector2 startPos)
    {
        position  = startPos;
        texture   = tex;
        moveSpeed = 5.0f;
    }
    public void Update(GameTime gameTime)
    {
        //------------------------------------------
        //check for keyboard input(keyboard IF statements)

    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, position, Color.White);
    }
}

回答1:


Your class is player but the constructor is Player, because they are different it is expecting Player to be a method rather than a constructor

Change the class name to Player and you will be good




回答2:


Your class name is player with lower case. When the compiler finds the constructor for class Player (upper case), it thinks it is a method called Player without a return type specified.

So simply rename your class to uppercase Player. C# is case sensitive, so player and Player are two different things.




回答3:


Your class is lowercase player, your constructor is uppercase. Class name & Constructor should always be identical and are case-sensitive :)




回答4:


I guess it's a typo. Your class name is player is lower case and your constructor has a capital letter, so it's not seen as a cinstructor but as a method missing the void keyword.

EDIT: Sorry for what se4ems to be a repeated answer, seems like many people answered at almost the same moment ;)




回答5:


Your class name is "player", but the constructor name is "Player" (uppercase of p). So the constructor is considered as a method without a return type. Rename it as "player" and it will work :)



来源:https://stackoverflow.com/questions/22739932/visual-studio-says-method-must-have-a-return-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!