簡體   English   中英

object 具有與成員 function 不兼容的類型限定符。 為什么會出現這個錯誤?

[英]the object has type qualifiers that are not compatible with the member function. Why is this error showing up?

PVector operator + (const PVector& lhs, const PVector& rhs){
    return PVector(lhs.getX() + rhs.getX(), lhs.getY()+ rhs.getY());
}

當我使用 getX() 或 getY() function 時,在 lhs 和 rhs object 上出現錯誤。 function 不會對 object 進行任何更改,它只返回一個私有浮點值。 我想知道為什么會這樣? 我在 c++ 中編程不是很擅長,但我想學習。

我可以從 lhs 和 rhs object 中刪除 const 但我想知道為什么會出現此錯誤。

謝謝您的幫助。

此編譯錯誤表明您嘗試使用 const object 的(非常量)成員 function。

class A {
    void f(); 
}

const A a;
A.f(); // <- this will result in a cv qualifier compile error.

您可以通過將 function 設置為 const 來解決此問題

class A {
    void f() const; 
}

這意味着,此 function 調用不會更改任何成員變量。 因此,它可以應用於 const object。

您的 getX 和 getY 函數很可能需要為 const。

暫無
暫無

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

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