[英]Operator ( == ) overloading error
因此,我正在開發流程圖項目(OOP),並且需要實現GUI的條件以允許用戶使用DrawConnector,但是“ Connector”的條件是僅當您有另外2個“ Shape”時才繪制”(鑽石/ Rectangl /等等)。
這是連接器類.h
class Connector
{
protected :
Point start;
Point end;
Statement *St;
bool DrawCondition;
bool DelCondition;
public :
Connector();
virtual void setStart(Point S); //Not a condition to be virtuals
virtual void setEnd(Point E);
virtual void DrawConnector(Output* pOut);
//virtual bool setDrawCondition ();
friend bool operator == (Point P, Point T);
};
我實際上想在運行時通過鼠標上的值用戶“ Clicks”來“ setStart”和“ setEnd”,然后檢查這些點是否在形狀上,因此它繪制了連接器,如果沒有,則什么也沒發生。
void Connector::setStart(Point S)
{
if (S == St->getPoint())
{start = S;}
else return;
}
void Connector::DrawConnector(Output *pOut)
{
/*if (DrawCondition == true)*/
pOut->DrawConnector(start.x,start.y,end.x,end.y);
}
您可能會注意到,我重載了運算符“ ==”來檢查點是否已被讀取...但這最終給了我這個編譯錯誤!
錯誤3錯誤LNK2019:未解析的外部符號“布爾__cdecl運算符==(結構點,結構點)”(?? 8 @ YA_NUPoint @@ 0 @ Z)在函數“公共:虛擬void __thiscall Connector :: setStart(struct Point)中引用” )“(?setStart @ Connector @@ UAEXUPoint @@@@ Z)C:\\ Users \\ Cereal Killer \\ Downloads \\ Phase1-Code \\ Phase1-Code \\ Connector.obj
編輯
抱歉,我忘了提到我已經在另一個文件中定義了重載,但是我想這是問題所在,但是為什么不是“ GLOBAL FUNCTION”呢?
這是國防部。 :
bool operator == (Point P, Point T)
{
if ( (P.x == T.x) && (P.y == T.y) )
return true;
else return false;
}
還有一件事,如果我想通過GUI形狀“ DrawRectangle,DrawCircle等”檢查給定的POINT是否位於SHAPE上。 如何“追蹤繪制形狀的點”? 還是有另一種方法?
您已經聲明
friend bool operator == ( Point P, Point T);
但尚未定義。
您需要定義
bool operator == ( Point P, Point T) {
//...
}
您需要像任何函數一樣給運算符定義,而不僅僅是聲明。 缺少它會產生此錯誤。 因為它是一個朋友,所以它只是一個全局函數,您需要在類外部定義此函數。 在標題中,或在適當的.cpp文件中。
bool operator==(Point P, Point T) {
return P.x == T.x && P.y == T.y;
}
您可能還考慮通過const引用而不是值傳遞點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.