![]() |
#2
rjsp2014-09-09 12:22
换书吧,你这不是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; } |
#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)