小白在自学C++,练习题调试无能求解救
题目是关于线段的类,由键盘输入线段端点的坐标,返回线段起始点坐标和线段长度。然而初学不知道怎么搞得,在VS运行一堆问题= =1>Line.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall Point::~Point(void)" (??1Point@@QAE@XZ),该符号在函数 __unwindfunclet$??0Line@@QAE@XZ$0 中被引用
1>test.obj : error LNK2001: 无法解析的外部符号 "public: __thiscall Point::~Point(void)" (??1Point@@QAE@XZ)
1>Line.obj : error LNK2019: 无法解析的外部符号 "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Point::ToString(class Point const &)const " (?ToString@Point@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV1@@Z),该符号在函数 "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall Line::ToString(class Line const &)const " (?ToString@Line@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV1@@Z) 中被引用
1>Line.obj : error LNK2019: 无法解析的外部符号 "public: double __thiscall Point::Distance(class Point)" (?Distance@Point@@QAENV1@@Z),该符号在函数 "double __cdecl Length(void)" (?Length@@YANXZ) 中被引用
1>test.obj : error LNK2019: 无法解析的外部符号 "public: double __thiscall Line::Length(void)const " (?Length@Line@@QBENXZ),该符号在函数 _main 中被引用
1>test.obj : error LNK2019: 无法解析的外部符号 "public: double __thiscall Point::X(void)const " (?X@Point@@QBENXZ),该符号在函数 _main 中被引用
1>test.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall Point::Y(double)" (?Y@Point@@QAEXN@Z),该符号在函数 _main 中被引用
1>test.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall Point::X(double)" (?X@Point@@QAEXN@Z),该符号在函数 _main 中被引用
1>D:Level3\HW\2.3.5\Debug\2.3.5.exe : fatal error LNK1120: 7 个无法解析的外部命令
// Point.hpp
#ifndef Point_HPP
#define Point_HPP
#include <iostream>
using namespace std;
class Point
{
private:
double x; // X coordinate
double y; // Y coordinate
public:
// Constructors
Point(); // Default constructor
Point(double xval, double yval); // Initialize with x and y value
~Point();
// Accessing functions
double X() const ; // The x-coordinate
void X(double newX);
double Y() const; // The y-coordinate
void Y(double newY);
string ToString(const Point &pt)const;//a string description of the point
double Distance(const Point p);//Calculate the distance between two points
};
#endif
// Line.hpp
//
// (Finite) line segments in 2 dimensions. This class represents an undirected
// line segment.
#ifndef Line_HPP
#define Line_HPP
#include <iostream>
#include "Point.hpp"
using namespace std;
class Line
{
private:
Point startPoint; // e1
Point endPoint; // e2
public:
// Constructors
Line(); // Line with both end Points at the origin
Line(const Point &p1, const Point &p2); // Line with end Points [p1, p2]
Line(const Line &l); // Copy constructor
virtual ~Line(); // Destructor
// Accesssing functions, point getter
Point start() const;
Point end() const;
// Modifiers, point setter
void start(const Point& pt); // Set Point pt1
void end(const Point& pt); // Set Point pt2
string ToString(const Line &l)const;//a string description of the line
double Length() const;//the length of the line
};
#endif
//Line.cpp
#include<iostream>
#include<sstream>
#include <cmath>
#include "Line.hpp"
Point::Point():x(0),y(0)
{
}
Line::Line()
{
}
Line::~Line()
{
}
Line::Line(const Point& p1, const Point& p2)
{
startPoint=p1;
endPoint=p2;
}
Line::Line(const Line& l)
{
startPoint=l.startPoint;
endPoint=l.endPoint;
}
Point Line::start() const
{
return startPoint;
}
Point Line::end() const
{
return endPoint;
}
void Line::start(const Point& pt)
{
startPoint=pt;
}
void Line::end(const Point& pt)
{
endPoint=pt;
}
string Line::ToString(const Line &l)const
{
stringstream ss;
ss<<"Start Point:"<<start().ToString(startPoint)<<"End Point"<<end().ToString(endPoint);
return ss.str();
}
double Length()
{
double d;
const Line l;
d=l.start().Distance(l.end());
return d;
}
// Testpoint.cpp
#include <iostream>
#include "Line.hpp"
using namespace std;
int main()
{
double x1,y1,x2,y2;
cout<<"enter the coordinates of the start point and end point respectively:\n";
cin>>x1;
cin>>y1;
cin>>x2;
cin>>y2;
// Create a point
Point p1,p2;
p1.X(x1);
p1.Y(y1);
p2.X(x2);
p2.Y(y2);
double d = p1.X();
// Linesegment
Line L1(p1, p2);
//cout<<"The description of the line: "<<L1.ToString(L1)<<endl;
cout<<"The length of the line is: "<<L1.Length()<<endl;
return 0;
}