1

ここで何が間違っているのかわかりません。
リストをソートし、それをソートするための比較機能が必要です。
私の問題が正確に解決されたコード例を見つけましたが、うまくいきません。

私はいつもこのエラーを受け取ります:
エラー: ISO C++ は型のない 'Cell' の宣言を禁止しています

セルは私のタイプで はありませんか?

AStarPlanner.h

class AStarPlanner {

public:

  AStarPlanner();

  virtual ~AStarPlanner();

protected:

  bool compare(const Cell& first, const Cell& second);

  struct Cell {

        int x_;
        int y_;
        int f_;   // f = g + h
        int g_;   // g = cost so far
        int h_;   // h = predicted extra cost

        Cell(int x, int y, int g, int h) : x_(x), y_(y), g_(g), h_(h) {
                f_ = g_ + h_;
        }
  };

};

AStarPlanner.cpp

 bool AStarPlanner::compare(const Cell& first, const Cell& second)
 {
    if (first.f_ < second.f_)
       return true;
    else
       return false;
 }
4

2 に答える 2

7

Cellの宣言をメソッド宣言の前に移動します。

class AStarPlanner {
public:
  AStarPlanner();
  virtual ~AStarPlanner();
protected:
  struct Cell {
        int x_;
        int y_;
        int f_;   // f = g + h
        int g_;   // g = cost so far
        int h_;   // h = predicted extra cost
        Cell(int x, int y, int g, int h) : x_(x), y_(y), g_(g), h_(h) {
                f_ = g_ + h_;
        }
  };
  bool compare(const Cell& first, const Cell& second);
};

また、技術的にはCell型はありませんがAStarPlanner::Cell(ただし、 のコンテキストで自動的に解決されますclass)。

于 2012-05-21T07:47:31.847 に答える
4

クラス宣言内の名前の可視性に関する C++ のルールは明白ではありません。たとえば、メンバーがクラスで後で宣言されている場合でも、メンバーを参照するメソッド実装を持つことができます...しかし、型が後で宣言されている場合、ネストされた型を参照するメソッド宣言を持つことはできません。

クラスの先頭にネストされた型宣言を移動すると、問題が解決します。

この制限には技術的な理由はありません (または、より正確に言うと、それが何であるかはわかりませんが、完全な C++ パーサーをあえて書くことはありませんでした)。ただし、これは言語の定義方法です。

于 2012-05-21T08:02:49.363 に答える