-3

我已经阅读了其他一些相关的帖子,但我不相信我有和他们一样的问题。我相信我正在为我的继承类正确地做我的构造函数,但是它仍然无法工作 - 甚至不会认识到我在那里似乎有一个构造函数。

class BlockedNumber : PhoneNumber
{
    public BlockedNumber(string a, string m, string l)
        : base(a, m, l) { }
}

这仍然给了我标题中的错误:

“DTS.PhoneNumber 不包含采用 0 个参数的构造函数。

我不知道为什么它不能正确识别我的构造函数。错误(VS12 中的蓝色下划线)出现在第一次BlockedNumber使用class.

有谁知道为什么它不喜欢那样?

4

4 回答 4

2

以下编译

class BlockedNumber : PhoneNumber
{
    public BlockedNumber(string a, string m, string l)
        : base(a, m, l) { }
}

internal class PhoneNumber
{
    public PhoneNumber(string a, string m, string l) { }
}

你的问题在别处。很可能您正在PhoneNumber用 0 个参数实例化一个其他地方。

于 2013-03-22T21:33:47.663 回答
2

如果不直接指定: base(x,y,z)调用哪个父构造函数,编译器会尝试寻找一个没有参数的父构造函数默认调用。

it is explained in this post - C# Error: Parent does not contain a constructor that takes 0 arguments

于 2013-08-16T17:46:27.387 回答
0

您的子类很可能还有另一个您没有透露的无参数构造函数,但该构造函数不会以显式方式调用基类构造函数。因此,您认为它不相关。但是,C# 会向每个构造函数插入一个基类构造函数调用,因为这是 CIL 所必需的。因此,另一个构造函数抱怨它无法找到要调用的基类构造函数。

于 2013-03-22T21:41:39.993 回答
-1

从 VS2010 获取你所拥有的并生成一个类

internal class PhoneNumber
    {
        private string a;
        private string m;
        private string l;

        public PhoneNumber(string a, string m, string l)
        {
            // TODO: Complete member initialization
            this.a = a;
            this.m = m;
            this.l = l;
        }
    }

class BlockedNumber : PhoneNumber
{
    public BlockedNumber(string a, string m, string l)
        : base(a, m, l) { }
}

This code compiles just fine so just like Yuriy stated the issue must be somewhere else.

于 2013-03-22T21:39:33.360 回答