1

当试图为具有相同类名的类创建构造函数时。为什么会抛出如下错误:

" Error: 'StaveProcessor': member names cannot be the same as their enclosing type"

代码 :

namespace ImageProcessing
{
    class StaveProcessor
    {
        public Bitmap stave;

        public StaveProcessor(Bitmap image) //constructor
        {
            stave = image;
        }
    }
}

如何解决这个问题并创建构造函数?

ps:请认为我自己不是专家,请原谅我问愚蠢的问题,并帮助我学习和识别。谢谢你

4

2 回答 2

5

好吧,您提供的代码没有任何问题(它可以编译),如果您执行以下操作,则会显示您得到的异常:

class StaveProcessor
{
    // can't have this method with this name (note the void, it's not a constructor)
    public void StaveProcessor(Bitmap image)
    {
        stave = image;
    }
}
于 2013-07-07T06:03:22.897 回答
1

@hirosht 你的代码没有任何错误:看到这个页面有同样的问题

答案是(我认为)您已经使用类型声明了构造函数,例如 void 或 int 或其他类型。

于 2013-07-07T06:21:21.673 回答