【问题标题】:The name "Word" is not a class/type名称“Word”不是类/类型
【发布时间】:2014-06-15 19:05:34
【问题描述】:

我在 Dart 中有三个类,如下所示:

位:

part of CB_Crypto;

class Bit {
  bool _state = false,
       _sympathetic = false;
  int _alignment = 0;

  Bit(List args) {
    _state = args[0];
    _sympathetic = args[1];
    _alignment = args[2];
  }

  bool state() => _state;

  bool not() => _state = !_state;

  bool set(bool state) => _state = state;

  bool isSympathetic() =>  _sympathetic;

  operator &(Bit b) => state() && b.state();

  operator |(Bit b) => state() || b.state();
}

单词:

part of CB_Crypto;

class Word {
  List<Bit> _bits = [];

  Word(List<List> bits) {
    bits.forEach((bit) => _bits.add(new Bit(bit)));
  }

  bool not(int i) => _bits[i].not();

  void notAll() => _bits.forEach((bit) => bit.not());

}

表:

part of CB_Crypto;

class Table {
  List<Word> _words;

  Word(List<List> words) {
    words.forEach((word) => _words.add(new Word(word)));
  }
}

这些都在不同的文件中,并且是同一个库的一部分。但是,我在 Table 类中收到来自 Dartium 的警告,要求我使用 Word 作为类型并尝试实例化 Word 对象:the name "Word" is not a (type/class) and cannot be used as a parametrized type

我相信我编写了类似于 Bit 类的 Word 类,并且在使用 Word 时在 Table 中使用时不会引发错误。我错过了什么吗?

【问题讨论】:

  • 您能否显示引发此错误的代码?
  • 我想知道这是否有效:Word(List&lt;List&gt; words) { - 为了更好地理解,您应该为我们输入(内部)列表。
  • 您是否将每个部分的part "file.dart" 添加到您的库中?
  • @Robert 引发错误的代码是 Table 类(列表定义和 forEach 循环。
  • @GünterZöchbauer 是的,我做到了。

标签: list class dart


【解决方案1】:

您正在使用 Word 作为 Table 类中的构造函数。

Word(List<List> words) {
  words.forEach((word) => _words.add(new Word(word)));
}

从外观上看,您的构造函数应该是 Table

Table(List<List> words) {
  words.forEach((word) => _words.add(new Word(word)));
}

【讨论】:

  • 我不敢相信我没有注意到这一点。我确实从 Word 中复制并过去了 Table 的代码并更改了所有名称,因为它们本质上是相同的。我想我错过了一个:) 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 2020-03-05
  • 2013-08-05
  • 1970-01-01
  • 2021-07-14
  • 2012-08-11
相关资源
最近更新 更多