關於友元類的一些東西

來源:互聯網
上載者:User

對於友元類,大家應該不陌生了,友元類聲明會將整個類說明成為另一個類的友元關係,友元類的所有成員函數都可以是另一個類的友元函數;昨天在偵錯工具的時候,當我把所有程式寫在一個檔案中的時候,包括類和友元類的實現,還有main函數,編譯能通過,而且也能執行,但是當我把類的定義和實現分開來的時候,就編譯通不過了,具體代碼如下

myPoint.h

#ifndef _myPoint_H_#define _myPoint_H_//#include "Line.h"//這裡如果加上這條就會編譯通不過,去掉後就能編譯通過,也可以執行,不知道為什麼class line;class myPoint{private:float x;float y;static int count;public:myPoint();~myPoint();myPoint(myPoint& point);myPoint(float x, float y);void display();friend class Line;friend float fdis(myPoint& p1, myPoint& p2);};#endif

myPoint.cpp

#include "stdafx.h"#include <iostream>#include <math.h>#include "myPoint.h"using namespace std;int myPoint::count = 0;myPoint::myPoint(void){this->count++;}myPoint::myPoint(myPoint& p){this->x = p.x;this->y = p.y;this->count++;}myPoint::myPoint(float x, float y){this->x = x;this->y = y;this->count++;}myPoint::~myPoint(void){}void myPoint::display(){cout<<"myPoint:("<<this->x<<","<<this->y<<")"<<endl;cout<<"當前是第"<<this->count<<"座標點"<<endl;}float fdis(myPoint& p1, myPoint& p2){float f1 = p1.x - p2.x;float f2 = p2.y - p2.y;float d = pow(f1, 2) +  pow(f2, 2);d = sqrt(d);return d;}

Line.h

#ifndef _LINE_H_#define _LINE_H_#include "myPoint.h"class Line{public:Line(){}~Line(){}Line(Line& line);Line(myPoint& pp1, myPoint& pp2);float getLength();private:myPoint p1;myPoint p2;};#endif

Line.cpp

#include "stdafx.h"#include <iostream>#include <math.h>#include "Line.h"using namespace std;Line::Line(Line &line){this->p1 = line.p1;this->p2 = line.p2;}Line::Line(myPoint& pp1, myPoint& pp2){this->p1 = pp1;this->p2 = pp2;}float Line::getLength(){float f1 = this->p1.x - this->p2.x;float f2 = this->p1.y - this->p2.y;float d = pow(f1, 2) +  pow(f2, 2);d = sqrt(d);return d;}

有誰知道為什麼在myPoint.h中加上#include "Line.h"會編譯通不過?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.