标题:求教关于类和对象的例子,求平面内两条直线的交点,已知直线斜截式方程
只看楼主
cuixing158
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-9-8
结帖率:100%
 问题点数:0 回复次数:1 
求教关于类和对象的例子,求平面内两条直线的交点,已知直线斜截式方程
#include <iostream>
using namespace std;
struct point
{
    double x,y;//交点坐标
};

class line
{
private:
    double m,b;//m为斜率,b为截距
    point pt;//pt为交点
public:
    void set_line_data();
    void show_line_data();
    void calc_pt(line);
};

void line::set_line_data()
{
    cout<<"please input m(xielv):";
    cin>>m;
    cout<<"please input b(jieju):";
    cin>>b;
}
void line::show_line_data()
{
    cout<<"m="<<m<<",b="<<b<<endl;
}
void line::calc_pt()
{
    pt.x=(crossing_line.b-b)/(m-crossing_line.m);
    pt.y=m*pt.x+b;
    cout<<"the two lines crossing point is:("<<pt.x<<","<<pt.y<<")"<<endl;

}

int main()
{
    line line1,line2;
    cout<<"please input line1:\n"; line1.set_line_data();
cout<<"please input line2:\n"; line2.set_line_data();
cout<<"line1:\n";   line1.show_line_data();
cout<<"line2:\n";   line2.show_line_data();
line1.calc_pt(line2);
return 0;
}

错误提示是:
--------------------Configuration: example2 - Win32 Debug--------------------
Compiling...
example2.cpp
d:\my_c_documents\example\example2.cpp(31) : error C2511: 'calc_pt' : overloaded member function 'void (void)' not found in 'line'
        d:\my_c_documents\example\example2.cpp(9) : see declaration of 'line'
执行 cl.exe 时出错.

example2.obj - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: private include public please double 
2014-09-09 10:47
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
换书吧,你这不是C++的行事方式
程序代码:
#include <iostream>
#include <stdexcept>

struct point
{
    double x, y;

    friend std::ostream& operator<<( std::ostream& os, const point& pt );
};

std::ostream& operator<<( std::ostream& os, const point& pt )
{
    return os << pt.x << ' ' << pt.y;
}

class line
{
public:
    explicit line( double m=0, double b=0 ) : m_(m), b_(b)
    {
    }
private:
    double m_, b_;

    friend point point_of_intersection( const line& a, const line& b );
    friend std::ostream& operator<<( std::ostream& os, const line& ln );
    friend std::istream& operator>>( std::istream& is, line& ln );
};

std::ostream& operator<<( std::ostream& os, const line& ln )
{
    return os << ln.m_ << ' ' << ln.b_;
}

std::istream& operator>>( std::istream& is, line& ln )
{
    return is >> ln.m_ >> ln.b_;
}

point point_of_intersection( const line& a, const line& b )
{
    if( a.m_ == b.m_ ) throw std::domain_error("divide by zero");
    point pt = { (b.b_-a.b_)/(a.m_-b.m_), (a.m_*b.b_-b.m_*a.b_)/(a.m_-b.m_) };
    return pt;
}

using namespace std;

int main()
{
    line line1, line2;
    cout << "please input line1: ";
    cin >> line1;
    cout << "please input line2: ";
    cin >> line2;

    cout << "line1: " << line1 << endl;
    cout << "line2: " << line2 << endl;
    try {
        cout << "The point of intersection is: " << point_of_intersection(line1,line2) << endl;
    } catch( ... ) {
    }

    return 0;
}

2014-09-09 12:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-435873-1-1.html




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

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