| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 493 人关注过本帖
标题:求助:书上示例一句编译时出错如何改?
只看楼主 加入收藏
bardon
Rank: 2
等 级:论坛游民
帖 子:22
专家分:16
注 册:2011-1-16
结帖率:75%
收藏
已结贴  问题点数:20 回复次数:5 
求助:书上示例一句编译时出错如何改?
这是自考教材上的有关用友元函数求两点间距离的程序:
class Location{
private: float X,Y;
public:
    Location(float xi,float yi){X=xi;Y=yi;}
    float GetX(){return X;}
    float GetY(){return Y;}
    friend float distance(Location& a,Location& b);
};
   
        float distance(Location& a,Location& b)
    {
        float dx=a.X-b.X;
        float dy=a.Y-b.Y;
        return sqrt(dx*dx+dy*dy);
        
    }

void main(){

    Location p1(6,9),p2(9,6);
  float d = distance(p1,p2);
//  仅上句编译不能通过,请大虾指点如何修改,谢谢。
    cout<<"The distance is:"<<d<<endl;
}
搜索更多相关主题的帖子: friend 
2011-02-13 22:04
BlueSKySea
Rank: 2
等 级:论坛游民
帖 子:2
专家分:20
注 册:2011-2-8
收藏
得分:10 
没错啊,在2008里试了下,是可以的啊
// newtest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <math.h>

class Location
{
private: float X,Y;
public:
    Location(float xi,float yi){X=xi;Y=yi;}
    float GetX(){return X;}
    float GetY(){return Y;}
    friend float distance(Location& a,Location& b);
};

float distance(Location& a,Location& b)
{
    float dx=a.X-b.X;
    float dy=a.Y-b.Y;
    return sqrt(dx*dx+dy*dy);

}

void main(){

    Location p1(6,9),p2(9,6);
    float d = distance(p1,p2);
    //  仅上句编译不能通过,请大虾指点如何修改,谢谢。
    std::cout<<"The distance is:"<<d<<std::endl;
}
收到的鲜花
  • bardon2011-02-15 11:08 送鲜花  5朵  
2011-02-14 00:25
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:2 
你的编译器报的什么错?
收到的鲜花
  • bardon2011-02-15 11:09 送鲜花  3朵  
2011-02-14 01:40
bardon
Rank: 2
等 级:论坛游民
帖 子:22
专家分:16
注 册:2011-1-16
收藏
得分:0 
就那一行程序,报了很多行错,故也就不知是说些什么了。
用的是Visual Studio 2010,也许是编程软件的问题?
谢谢各位指点,再找一个编程软件试试看。

[ 本帖最后由 bardon 于 2011-2-14 23:24 编辑 ]
2011-02-14 23:21
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:8 
程序代码:
class Location
{
    friend double distance( const Location& a, const Location& b );
public:
    Location( double x, double y ) : x_(x), y_(y) {}
    double GetX() const { return x_; }
    double& GetX() { return x_; }
    double GetY() const { return y_; }
    double& GetY() { return y_; }
private:
    double x_, y_;
};

#include <cmath>
double distance( const Location& a, const Location& b )
{
    return sqrt( (a.x_-b.x_)*(a.x_-b.x_) + (a.y_-b.y_)*(a.y_-b.y_) );
}

#include <iostream>
using namespace std;

int main()
{
    Location p1(6.0,9.0), p2(9.0,6.0);
    double d = ::distance(p1,p2); // 使用 ::distance 以便和 std::distance 区分开来
    cout << "The distance is:" << d <<endl;

    return 0;
}

收到的鲜花
  • bardon2011-02-15 11:09 送鲜花  5朵  
  • bardon2011-02-15 11:09 送鲜花  5朵  
2011-02-15 08:19
bardon
Rank: 2
等 级:论坛游民
帖 子:22
专家分:16
注 册:2011-1-16
收藏
得分:0 
回复 5楼 rjsp
谢谢楼上大虾指点,用修改后的语句就能编译通过了。
若是不修改,改用另一编程软件“C与C++程序设计学习与实验系统 2011”
也能编译通过,谢谢各位指点。
2011-02-15 11:02
快速回复:求助:书上示例一句编译时出错如何改?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028346 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved