下面的程序怎样修改才没错 在v6下
#include<iostream>
#include<sstream>
using namespace std;
class Point
{
friend ostream& operator<<(ostream&,const Point&);
public:
Point(double=0.0,double=0.0);
Point(const Point&);
~Point();
Point& operator=(const Point&);
double x() const;
double y() const;
string toString() const;
protected:
double _x,_y;
};
#include"Point1.h"
class Line
{ friend class Point;
friend ostream& operator<<(ostream&,const Line&);
public:
Line(double=1.0,double=1.0);
Line(const Point&,double=0.0);
Line(const Point&,const Point&);
Line(double,double,double);
Line(const Line&);
~Line();
Line& operator=(const Line&);
double xCoef() const;
double yCoef() const;
double cTerm() const;
string toString() const;
protected:
double _a,_b,_c;
};
#include"Line.h"
ostream& operator<<(ostream& ostr,const Point& point)
{
return ostr<<point.toString();
}
Point::Point(double x,double y):_x(x),_y(y){ }
Point::Point(const Point& p):_x(p._x),_y(p._y){ }
Point::~Point(){};
Point& Point::operator=(const Point& p)
{
_x=p._x;
_y=p._y;
return *this;
}
double Point::x() const{return _x;}
double Point::y() const{return _y;}
string Point::toString() const
{ostringstream output;
output<<"("<<_x<<","<<_y<<")";
return output.str();
}
ostream& operator<<(ostream& ostr,const Line& line)
{
return ostr<<line.toString();
}
Line::Line(double x0,double y0):_a(y0),_b(x0),_c(-x0*y0){}
Line::Line(const Point& p,double m):_a(m),_b(-1),_c(p._y-m*p._x){}
Line::Line(const Point& p,const Point& q):_a(q._y-p._y),_b(p._x-q._x),_c(q._x*p._y-p._x*q._y){}
Line::Line(double a,double b,double c):_a(a),_b(b),_c(c){}
Line::Line(const Line& l):_a(l._a),_b(l._b)_c(l._c){}
Line::~Line(){}
Line& Line::operator=(const Line& line)
{
_a=line._a;
_b=line._b;
_c=line._c;
return *this;
}
double Line::xCoef() const{return _a;}
double Line::yCoef() const{return _b;}
double Line::cTerm() const{return _c;}
string Line::toString() const
{
ostringstream out;
out<<_a<<"x+"<<_b<<"y+"<<_c<<"=0";
return out.str();
}
int main()
{
Point p1(4,1);
cout<<"p1="<<p1<<"\n";
Line line1(2,3,6);
cout<<"line1:"<<line1<<"\n";
Line line2(-2,12);
cout<<"line2:"<<line2<<"\n";
Line line3(p1,3);
cout<<"line3:"<<line3<<"\n";
Point p2(0,5);
cout<<"p2="<<p2<<"\n";
Line line4(p1,p2);
cout<<"line4:"<<line4<<"\n";
}
前面2个作为头文件 后面的是cpp文件 怎么调式有错误 该怎么修改呢??、
#include<sstream>
using namespace std;
class Point
{
friend ostream& operator<<(ostream&,const Point&);
public:
Point(double=0.0,double=0.0);
Point(const Point&);
~Point();
Point& operator=(const Point&);
double x() const;
double y() const;
string toString() const;
protected:
double _x,_y;
};
#include"Point1.h"
class Line
{ friend class Point;
friend ostream& operator<<(ostream&,const Line&);
public:
Line(double=1.0,double=1.0);
Line(const Point&,double=0.0);
Line(const Point&,const Point&);
Line(double,double,double);
Line(const Line&);
~Line();
Line& operator=(const Line&);
double xCoef() const;
double yCoef() const;
double cTerm() const;
string toString() const;
protected:
double _a,_b,_c;
};
#include"Line.h"
ostream& operator<<(ostream& ostr,const Point& point)
{
return ostr<<point.toString();
}
Point::Point(double x,double y):_x(x),_y(y){ }
Point::Point(const Point& p):_x(p._x),_y(p._y){ }
Point::~Point(){};
Point& Point::operator=(const Point& p)
{
_x=p._x;
_y=p._y;
return *this;
}
double Point::x() const{return _x;}
double Point::y() const{return _y;}
string Point::toString() const
{ostringstream output;
output<<"("<<_x<<","<<_y<<")";
return output.str();
}
ostream& operator<<(ostream& ostr,const Line& line)
{
return ostr<<line.toString();
}
Line::Line(double x0,double y0):_a(y0),_b(x0),_c(-x0*y0){}
Line::Line(const Point& p,double m):_a(m),_b(-1),_c(p._y-m*p._x){}
Line::Line(const Point& p,const Point& q):_a(q._y-p._y),_b(p._x-q._x),_c(q._x*p._y-p._x*q._y){}
Line::Line(double a,double b,double c):_a(a),_b(b),_c(c){}
Line::Line(const Line& l):_a(l._a),_b(l._b)_c(l._c){}
Line::~Line(){}
Line& Line::operator=(const Line& line)
{
_a=line._a;
_b=line._b;
_c=line._c;
return *this;
}
double Line::xCoef() const{return _a;}
double Line::yCoef() const{return _b;}
double Line::cTerm() const{return _c;}
string Line::toString() const
{
ostringstream out;
out<<_a<<"x+"<<_b<<"y+"<<_c<<"=0";
return out.str();
}
int main()
{
Point p1(4,1);
cout<<"p1="<<p1<<"\n";
Line line1(2,3,6);
cout<<"line1:"<<line1<<"\n";
Line line2(-2,12);
cout<<"line2:"<<line2<<"\n";
Line line3(p1,3);
cout<<"line3:"<<line3<<"\n";
Point p2(0,5);
cout<<"p2="<<p2<<"\n";
Line line4(p1,p2);
cout<<"line4:"<<line4<<"\n";
}
前面2个作为头文件 后面的是cpp文件 怎么调式有错误 该怎么修改呢??、
作者: kaydxh 发布时间: 2011-06-16
感觉这代码编译也过不了才对
ostream& operator<<(ostream& ostr,const Point& point)
应该放到namespace std里
ostream& operator<<(ostream& ostr,const Point& point)
应该放到namespace std里
作者: luciferisnotsatan 发布时间: 2011-06-16
Line::Line(const Line& l):_a(l._a),_b(l._b),_c(l._c){}
作者: lthyxy 发布时间: 2011-06-16
public:
double _x,_y;
double _x,_y;
作者: lthyxy 发布时间: 2011-06-16
C/C++ code
#include<iostream> #include<sstream> using namespace std; class Point { friend ostream& operator<<(ostream&,const Point&); public: Point(double=0.0,double=0.0); Point(const Point&); ~Point(); Point& operator=(const Point&); double x() const; double y() const; string toString() const; public: double _x,_y; }; class Line { friend class Point; friend ostream& operator<<(ostream&,const Line&); public: Line(double=1.0,double=1.0); Line(const Point&,double=0.0); Line(const Point&,const Point&); Line(double,double,double); Line(const Line&); ~Line(); Line& operator=(const Line&); double xCoef() const; double yCoef() const; double cTerm() const; string toString() const; protected: double _a,_b,_c; }; ostream& operator<<(ostream& ostr,const Point& point) { return ostr<<point.toString(); } Point::Point(double x,double y):_x(x),_y(y){ } Point::Point(const Point& p):_x(p._x),_y(p._y){ } Point::~Point(){}; Point& Point::operator=(const Point& p) { _x=p._x; _y=p._y; return *this; } double Point::x() const{return _x;} double Point::y() const{return _y;} string Point::toString() const {ostringstream output; output<<"("<<_x<<","<<_y<<")"; return output.str(); } ostream& operator<<(ostream& ostr,const Line& line) { return ostr<<line.toString(); } Line::Line(double x0,double y0):_a(y0),_b(x0),_c(-x0*y0){} Line::Line(const Point& p,double m):_a(m),_b(-1),_c(p._y-m*p._x){} Line::Line(const Point& p,const Point& q):_a(q._y-p._y),_b(p._x-q._x),_c(q._x*p._y-p._x*q._y){} Line::Line(double a,double b,double c):_a(a),_b(b),_c(c){} Line::Line(const Line& l):_a(l._a),_b(l._b),_c(l._c){} Line::~Line(){} Line& Line::operator=(const Line& line) { _a=line._a; _b=line._b; _c=line._c; return *this; } double Line::xCoef() const{return _a;} double Line::yCoef() const{return _b;} double Line::cTerm() const{return _c;} string Line::toString() const { ostringstream out; out<<_a<<"x+"<<_b<<"y+"<<_c<<"=0"; return out.str(); } int main() { Point p1(4,1); cout<<"p1="<<p1<<"\n"; Line line1(2,3,6); cout<<"line1:"<<line1<<"\n"; Line line2(-2,12); cout<<"line2:"<<line2<<"\n"; Line line3(p1,3); cout<<"line3:"<<line3<<"\n"; Point p2(0,5); cout<<"p2="<<p2<<"\n"; Line line4(p1,p2); cout<<"line4:"<<line4<<"\n"; return 0; }
作者: q191201771 发布时间: 2011-06-16
using namespace std;这个去掉,需要所有用到std的都加上std,比如std::cin
作者: bdmh 发布时间: 2011-06-16
F:\study\C++编程\Line2.cpp(95) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(97) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(99) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(101) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(103) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(105) : error C2593: 'operator <<' is ambiguous会出现这个错误 我的是vc6.0
问题在于主函数里
F:\study\C++编程\Line2.cpp(97) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(99) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(101) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(103) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(105) : error C2593: 'operator <<' is ambiguous会出现这个错误 我的是vc6.0
问题在于主函数里
作者: kaydxh 发布时间: 2011-06-16
F:\study\C++编程\Line2.cpp(95) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(97) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(99) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(101) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(103) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(105) : error C2593: 'operator <<' is ambiguous会出现这个错误 我的是vc6.0
问题在于主函数里
F:\study\C++编程\Line2.cpp(97) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(99) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(101) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(103) : error C2593: 'operator <<' is ambiguous
F:\study\C++编程\Line2.cpp(105) : error C2593: 'operator <<' is ambiguous会出现这个错误 我的是vc6.0
问题在于主函数里
作者: kaydxh 发布时间: 2011-06-16