C#で抽象クラスのコンパイルエラー

このQ&Aのポイント
  • C#で抽象クラスを作成している際に、IListを継承するコレクションでコンパイルエラーが発生します。
  • エラーの原因は、IListのインターフェースメンバーを適切に実装していないことです。
  • 具体的には、GetEnumerator()メソッドの戻り値の型が一致していないためにエラーが発生しています。
回答を見る
  • ベストアンサー

【C#】抽象クラスでコンパイルエラー

#その他カテに質問するべきたったかな・・・ OS:Linux i686 環境:MonoDevelop 2.2.2 (Microsoft VisualC#でもやってみましたが同じエラーがでます) とある抽象クラスを作っています。 そのクラスはIList(ジェネリック)を継承するコレクションです。 いろいろIListを実装していくと次のコンパイルエラーが出ます。 Error CS0738: `PSP.PSPMusicCollection' does not implement interface member `System.Collections.IEnumerable.GetEnumerator()' and the best implementing candidate `PSP.PSPMusicCollection.GetEnumerator()' return type `System.Collections.Generic.IEnumerator<PSPMusic>' does not match interface member return type `System.Collections.IEnumerator' (CS0738) (PSP) Error CS0738: `PSP.PSPMusicCollection' does not implement interface member `System.Collections.Generic.ICollection<PSPMusic>.Remove(PSPMusic)' and the best implementing candidate `PSP.PSPMusicCollection.Count' return type `int' does not match interface member return type `bool' (CS0738) (PSP) エラーの発生場所付近: public abstract void Remove (PSPMusic item); public abstract System.Collections.Generic.IEnumerator<PSPMusic> GetEnumerator (); どうしたら解消できるでしょうか。 あと2番目のエラーが謎なんですが、何故ここでboolが出てくるのでしょう?? 参考リンク IList http://msdn.microsoft.com/ja-jp/library/system.collections.ilist.aspx IList<> http://msdn.microsoft.com/ja-jp/library/5y536ey6.aspx

質問者が選んだベストアンサー

  • ベストアンサー
回答No.2

えーっと,だから「明示的に実装」するんですが。 MSDN: 明示的なインターフェイスの実装 (C# プログラミング ガイド) http://msdn.microsoft.com/ja-jp/library/ms173157.aspx

carrotsoft
質問者

お礼

ありがとうございます。 解決できました。 とても感謝しています。

その他の回答 (1)

回答No.1

まず,前提。 interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable interface IEnumerator<T> : IEnumerator interface System.Collection.Generic.IEnumerable<T> : IEnumerable { IEnumerator<T> GetEnumerator(); } interface System.Collection.IEnumerable { IEnumerator GetEnumerator(); } 一つ目は,IEnumerableのGetEnumeratorが実装されていないためのエラーです。 IEnumerable<T>を実装するクラスでの基本パターンと化していますが, public IEnumerator<T> GetEnumerator() { /* 実装 */ } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } のように,IEumerableのGetEnumeratorを明示的に実装し,IEnumerable<T>のGetEnumeratorを呼び出します。 # IEnumerator<T>はIEnumeratorを継承するので,そのまま返すことができる。 二つ目は,ICollection<T>のRemoveを実装していないがためのエラーです。 MSDN: ICollection(T).Remove メソッド (System.Collections.Generic) http://msdn.microsoft.com/ja-jp/library/bye7h94w.aspx removeの戻り値をboolに変更するか,ICollection<T>のRemoveを明示的に実装してください。

carrotsoft
質問者

お礼

ありがとうございます。 おかげさまでRemove()は解決できました! しかし、 どうやって引数が同じ、名前が同じGetEnumerator()メソッドを実装すればいいのでしょうか。 ご教授くださいm(__)m

関連するQ&A

  • C#でコンパイルができない。

    C#のベータ版をセットアップして以下のサンプルプログラムを コンパイルしたところ、エラーがでました。何がいけないのでしょうか?OSはWin98です。 css001.cs(7,3): error CS0117: 'System.Console' does not contain a definition for 'Writeline' using System; プログラムリスト public class CSS001 { public static void Main(string[] args) { Console.Writeline("Hello World"); } }

  • C# Listクラスにクラスを追加したいがエラー

    以下のようなコードを書いたのですが (A)(B)の箇所で (A) error CS1513: } が必要です。 (B) /error CS1022:型、名前空間の定義、またはファイルの終わりが必要です。 というエラーになるのですが、なにが間違っているのでしょうか。 --------------------------------------------------- using System; using System.Collections.Generic; namespace My {  class MyClass  {   public static void Main()   { // <--- (A)    public List<Book> myList = new List<Book>    {     new Book(){ title = "ABC", price = 1000 },     new Book(){ title = "DEF", price = 2000 }    };   }  }  class Book  {   //プロパティの自動実装   public string title { get; set; }   public decimal price { get; set; }  } } // <--- (B) ---------------------------------------------------

  • C#でエラーCS0165が出てしまいます。

    unityでC#スクリプトを書いています。 以下のことを書くと下から三行目の {int speed = speed + 1; }   でエラーCS0165が出てしまいます。 調べたのですが、よくわかりませんでした。 8行目に  int speed = 0; と書いているのですが、これでは割り当てられていないのでしょうか。文を修正していただければ幸いです。 using System.Collections; using System.Collections.Generic; using UnityEngine; public class a : MonoBehaviour { int speed = 0; void Start() { } void Update() { if (Input.GetKey(KeyCode.R)) {int speed = speed + 1; } } }

  • C#での関数テーブルの作り方

    同じ質問で昔以下の回答がありましたが、今はライブラリが進歩しているようなので、より新しい構文を使った書き方がありましたら、是非ご教授いただければと思います。 delegate bool Execute( string oprand ); class Entry {  static bool aaa(string op)   { System.Console.WriteLine("aaa "+op); return true; }  static bool bbb(string op)   { System.Console.WriteLine("bbb "+op); return true; }  static bool ccc(string op)   { System.Console.WriteLine("ccc "+op); return true; }  static public void Main() {   System.Collections.Generic.Dictionary<string,Execute> map =    new System.Collections.Generic.Dictionary<string,Execute>();   map["AAA"] = aaa;   map["BBB"] = bbb;   map["CCC"] = ccc;   map["BBB"]("argument");  } } 宜しくお願い致します。

  • C#で構造体配列

    C#で構造体配列の操作練習?をしているのですが using System; using System.Collections.Generic; using System.Text; namespace test1 { public struct Data { public string name; // 名前 public uint value; // 値 } class read { Data[] human = new Data[300]; public static string idSearch() { for (int i = 0; i < 300; i++) { if (test1.read.human[i].value == 25) return test1.read.human[i].name; } } } } だと エラー CS0120: 静的でないフィールド、メソッド、またはプロパティ 'test1.read.human' で、オブジェクト参照が必要です。 と言うエラーが出るのですが、どうしてでしょうか?

  • C#についての質問です

    Microsoft Visual C# 2010 Express をつかって using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Console { class Program { static void Main(string[] args) { Console.WriteLine("皆様、はじめまして"); } } } と入力して.csで保存したのですが、コマンドプロンプトで 'csc' は、内部コマンドまたは外部コマンド、 操作可能なプログラムまたはバッチ ファイルとして認識されていません。 とでてコンパイルできません。対応を教えてください。

  • jspのクラスのコンパイルエラー

    jspのクラスのコンパイルエラーが解決出来ず皆様にお力を借りたいと思っています 以前はjdk1.4のtomcat5の動かしていたプログラムを jdk1.7のtomcat7で動かさないといけなくなりました。 実行環境 tomcat7 jdk1.7 エラー内容 org.apache.jasper.JasperException: JSPのクラスをコンパイルできません: An error occurred at line: [16] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The type AC10101_jsp must implement the inherited abstract method JspSourceDependent.getDependants() An error occurred at line: [22] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The type Map is not generic; it cannot be parameterized with arguments <String, Long> An error occurred at line: [25] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] _jspx_dependants cannot be resolved to a variable An error occurred at line: [25] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The type HashMap is not generic; it cannot be parameterized with arguments <String, Long> An error occurred at line: [26] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] _jspx_dependants cannot be resolved An error occurred at line: [26] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The method valueOf(String) in the type Long is not applicable for the arguments (long) An error occurred at line: [27] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] _jspx_dependants cannot be resolved An error occurred at line: [27] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The method valueOf(String) in the type Long is not applicable for the arguments (long) An error occurred at line: [28] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] _jspx_dependants cannot be resolved An error occurred at line: [28] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The method valueOf(String) in the type Long is not applicable for the arguments (long) An error occurred at line: [29] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] _jspx_dependants cannot be resolved An error occurred at line: [29] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The method valueOf(String) in the type Long is not applicable for the arguments (long) An error occurred at line: [30] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] _jspx_dependants cannot be resolved An error occurred at line: [30] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The method valueOf(String) in the type Long is not applicable for the arguments (long) An error occurred at line: [31] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] _jspx_dependants cannot be resolved An error occurred at line: [31] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The method valueOf(String) in the type Long is not applicable for the arguments (long) An error occurred at line: [45] in the generated java file: [E:\Tomcat 7.0\work\Catalina\localhost\test\org\apache\jsp\WEB_002dINF\pages\ac\AC10101_jsp.java] The type Map is not generic; it cannot be parameterized with a

  • Objective-C初心者です。コンパイルができません。

    Objective-C初心者です。コンパイルができません。 4月からC言語でプログラミングを学び始め、昨日からObjective-Cに手を出しました。 Mac OS X 10.6.4を使用中です。入門書に書いてあったコードをそのまま入力し、ターミナルからgccによるコンパイルを行ったところエラーがでます。 どなたか原因が分かる方、アドバイスをお願いします。 以下具体的なコードです。←で指した行(2箇所)にエラーメッセージが出ています。 Objectを定義した正しいインクルードファイルがない、などが原因かなと考え調べて見たのですがどうもobjc/Object.hで問題なさそうなので打つ手がなく困っています。 #import <stdio.h> #import <objc/Object.h> @interface Test : Object ←error: cannot find interface declaration for 'Object', superclass of 'Test - (void) method ; @end @implementation Test - (void) method  { printf("Kitty on your lap\n") ; } @end int main() { id obj = [Test alloc] ;   ←warning: 'Test' may not respond to '+alloc' [obj method]; return 0; }

  • 構造体のジェネリックのインスタンス化について

    先日、C++を始めたばかりの者です。 Visual C++ 2008を使って、Windowsフォームアプリケーションを作っているのですが、以下のように、 List<Point>^ pts = new List<Point>(); とすると、この行で、コンパイルエラー:C3845 "ref クラス または値型の内部で、スタティック データ メンバのみ初期化することができます"と、なってしまいます。 System::Collections::Genericと、System::Drawingはusing済みです。 回答、よろしくお願いします。

  • c++ クラスに関してです。

    うまくクラス同士を連動させることができなくてエラーが出てしまいます。 どこが間違えているのかアドバイスくださると助かります。 #ifndef H22_H_ #define H22_H_ #include <string> #include "account.h" class Bank { public: Bank(); void deposit(double amount, const std::string& accountType); void withdraw(double amount, const std::string& accountType); void transfer(double amount, const std::string& accountType); double getBalance() const; double getSavingsBalance() const; double getCheckingBalance() const; private: Account checking, savings; }; #endif #ifndef ACCOUNT_H #define ACCOUNT_H #include <string> #include "account.h" class Account{ public: Account(); Account(double &amout); double deposit(double amount); double getBalance() const; double withdraw(double amount); double balance; private: }; #endif #include <iostream> #include "acount.h" using namespace std; Account::Account { balance = 0; } Account::Account(double amount) { balance = amount; } Account::double deposit() const { return balance; } Account::double getBalance(double balance) { balance += amount; return balance; } Account::double withdraw(double amount) { if(amount > balance) { balance -= 5; return balance; } elese { balance -= amount; return balance; }} h22.h の中身 #include <string> #include "h22.h" #include "account.h" #include <stdexcept> using namespace std; Bank::Bank() { Account checking; Account saving; } void Bank::deposit(double amount, const string& accountType) { if (accountType != "S" && accountType != "C") throw invalid_argument("Does not compute"); else if(accountType == "S") savings.balance += amount; else if(accountType == "C") checking.balance += amount; } void Bank::withdraw(double amount, const string& accountType) { if (accountType != "S" && accountType != "C") throw invalid_argument("Does not compute"); else if(accountType == "S") savings.balance -= amount; else if(accountType == "C") checking.balance -= amount; } void Bank::transfer(double amount, const string& accountType) { if (accountType != "S" && accountType != "C") { throw invalid_argument("Does not compute");} else if(accountType == "S") { savings.balance += checking.balance; savings.balance = 0;} else if(accountType == "C") { checking.balance += amount; checking.balance = 0;} } double Bank::getBalance() const { return getBalance(); } double Bank::getSavingsBalance() const { return savings.getBalance(); } double Bank::getCheckingBalance() const { return checking.getBalance(); } メイン.cpp http://pastebin.com/rQTj8xciです。 なにかヒントやアドバイスお願いします

専門家に質問してみよう