簡體   English   中英

g++ 鏈接器找不到 const 成員函數

[英]g++ linker can't find const member function

我有一個 Point 類(具有整數成員 x 和 y),它有一個成員函數withinBounds ,聲明如下:

bool withinBounds(const Point&, const Point&) const;

並定義如下:

bool Point::withinBounds(const Point& TL, const Point& BR) const
{
    if(x < TL.getX()) return false;
    if(x > BR.getX()) return false;
    if(y < TL.getY()) return false;
    if(y > BR.getY()) return false;

    // Success
    return true;
}

然后在另一個文件中,我像這樣調用內withinBounds

Point pos = currentPlayer->getPosition();
if(pos.withinBounds(topleft, bottomright))
{
    // code block
}

這編譯得很好,但無法鏈接。 g++ 給了我這個錯誤:

/home/max/Desktop/Development/YARL/yarl/src/GameData.cpp:61: undefined reference to 'yarl::utility::Point::withinBounds(yarl::utility::Point const&, yarl::utility::Point const&)'

當我使函數不是 const 時,它鏈接正常。 有誰知道原因嗎? 鏈接器錯誤看起來像是在尋找函數的非常量版本,但我不知道為什么會這樣。

看起來在調用文件中,無論包含任何標頭以提供withinBounds的聲明,都具有該方法的非常量版本的不正確聲明(請注意,未定義的引用是對非常量版本的)。 確保您的項目包含目錄不包含多個版本的標頭。 還要確保您確實按照預期更改並保存了標題。

我只想在這里為未來的讀者添加,因為這是 google 上的一個接近結果,如果您確信您的定義是正確的,請嘗試刪除所有目標文件 (.o),然后重新編譯

暫無
暫無

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

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