簡體   English   中英

C ++禁止在將結構傳遞給函數時聲明沒有任何類型錯誤的…

[英]C++ forbids declaration of … with no type error while passing a struct to a function

我無法弄清楚我在做什么錯。
我想對列表進行排序,並具有一個比較功能對其進行排序。
找到了可以完全解決我的問題的代碼示例,但這對我不起作用。

我總是收到此錯誤:
錯誤:ISO C ++禁止聲明無類型的“單元格”

細胞不是我的類型嗎?

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;
 }

Cell的聲明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的上下文中自動解析)。

關於類聲明中名稱可見性的C ++規則並不明顯。 例如,即使該成員稍后在類中聲明,您也可以具有引用該成員的方法實現...但是,如果稍后聲明該類型,則您不能具有引用嵌套類型的方法聲明。

在類的開頭移動嵌套的類型聲明可以解決您的情況下的問題。

沒有這個限制的技術原因(或者說更好,我不知道它可能是什么,但我從來不敢寫一個完整的C ++解析器)。 但是,這就是語言的定義方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM