Undefined reference to my classes? C++ Beginner - c++

To get a bit of practice with OOP i'm trying to make a Point class (has 2 ints, x & y) and a Line class (has 2 Points).
Now when i go to build my main.cpp i get errors like..
"undefined reference to `Point::Point(float, float)' " and
" undefined reference to `Line::Line(Point, Point)'"
At a loss as to why, perhaps you could take a brief look at my files? It'd be much appreciated!
Main.cpp
#include "Point.hpp"
#include "Line.hpp"
#include <iostream>
using namespace std;
int main()
{
Point p1(2.0f, 8.0f); // should default to (0, 0) as specified
Point p2(4.0f, 10.0f); // should override default
p1.setX(17);
if ( p1.atOrigin() && p2.atOrigin() )
cout << "Both points are at origin!" << endl;
else
{
cout << "p1 = ( " << p1.getX() << " , " << p1.getY() << " )" <<endl;
cout << "p2 = ( " << p2.getX() << " , " << p2.getY() << " )" <<endl;
}
Line line(p1, p2);
Point midpoint = line.midpoint();
cout << "p1 = ( " << midpoint.getX() << " , " << midpoint.getY() << " )" <<endl;
return 0;
}
Line.hpp
#ifndef _LINE_HPP_
#define _LINE_HPP_
#include "Point.hpp"
class Line{
public:
Line(Point p1, Point p2);
//void setp1(Point p1);
//void setp2(Point p2);
//Point getp1 finish
Point midpoint();
int length();
private:
int _length;
Point _midpoint;
Point _p1, _p2;
};
#endif
Line.cpp
#include "Line.hpp"
#include <math.h>
Line::Line(Point p1, Point p2) : _p1(p1), _p2(p2)
{
}
Point Line::midpoint()
{
_midpoint.setX() = (_p1.getX()+ _p2.getX()) /2;
_midpoint.setY() = (_p1.getY()+ _p2.getY()) /2;
}
int Line::length()
{
//a^2 + b^2 = c^2
_length = sqrt( ( (pow( _p2.getX() - _p1.getX(), 2 ))
+(pow( _p2.getY() - _p1.getY(), 2 )) ) );
}
Point.hpp
#ifndef _POINT_HPP_
#define _POINT_HPP_
class Point {
public:
Point( float x = 0, float y = 0);
float getX() const;
float getY() const;
void setX(float x = 0);
void setY(float y = 0);
void setXY(float x = 0, float y = 0);
bool atOrigin() const;
private:
float _x, _y;
};
#endif
Point.cpp
#include "Point.hpp"
Point::Point(float x, float y) : _x(x), _y(y)
{
}
float Point::getX() const
{
return _x;
}
float Point::getY() const
{
return _y;
}
void Point::setX(float x)
{
//if (x >= 0 &&
_x = x;
}
void Point::setY(float y)
{
//might want to check
_y = y;
}
void Point::setXY(float x , float y )
{
setX(x);
setY(y);
}
bool Point::atOrigin() const
{
if ( _x == 0 && _y == 0)
return true;
return false;
}

In C++, not only do you have to compile main.cpp, but you also have to compile your Line.cpp and Point.cpp files. Then, when you have them all compiled into object files, you must link the object files together. This is handled automatically by some other languages such as Java.
The exact instructions on how to do this will depend on which development environment you are using.

Your Point.cpp isn't being compiled or given to the linker, try including it in your build.

Related

How to use overload operator as condition in a if statment?

Here is the class
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <iomanip>
#include <iostream>
using namespace std;
class Point {
protected:
int x, y;
double operator-(const Point &def){
return sqrt(pow((x-def.x),2.0)+
pow((y-def.y),2.0));
}
};
class Circle: public Point {
private:
int radius;
public:
Circle(){
this->x=x;
this->y=y;
this->radius=radius;
}
Circle(int x, int y, int radius){
this->x=x;
this->y=y;
this->radius=radius;
}
void printCircleInfo() {
cout << x << " " << y << " " << radius << " " ;
}
This is the operator I want to be the condition in my if statement.
bool operator==(const Circle &def){
return (x==def.x) & (y==def.y) & (radius==def.radius);
}
bool doIBumpIntoAnotherCircle(Circle anotherCircle){
if (anotherCircle.radius + radius >= *this - anotherCircle )
return true;
return false;
}
};
Here is main
int main(){
int x,y,radius;
const int SIZE = 13;
Circle myCircleArry[SIZE];
myCircleArry[0] = Circle(5,3,9);
cout << endl;
myCircleArry[0].printCircleInfo(); cout << " ; ";
ifstream Lab6DataFileHandle;
Lab6DataFileHandle.open("Lab6Data.txt");
while (!Lab6DataFileHandle.eof( )) {
for (int i = 1; i < SIZE; i++) {
Lab6DataFileHandle>>x;
Lab6DataFileHandle>>y;
Lab6DataFileHandle>>radius;
myCircleArry[i] = Circle(x,y,radius);
if (myCircleArry[0].doIBumpIntoAnotherCircle(myCircleArry[i])) {
myCircleArry[i].printCircleInfo(); cout << " ; ";
Here is the If statement
if ( operator==( Circle &def))
{cout <<"*";
}
}
}
}
Lab6DataFileHandle.close();
}
How do I use the overloaded operator as the condition of the if statement? If you need any clarification just ask other wise please leave an example in your answer.
Thank you for your time.
A == needs two arguments (even if the overload is a member), you would write the if as any other if statement:
if(circle1 == circle2) { ... }
and if there's a matching overload the compiler would transform that into something like:
if(circle1.operator ==(circle2)) { ... }

Storing Cartesian points in a vector and outputting distance

Im having trouble writing my main for a class I created. I created a class called CartesianPoints which I want to use to construct my main with. I have the general structure of my main created but am struggling with the some technical stuff..
Heres what im looking for:
creating an empty vector of CartesianPoint Objects which will be the starting point for the vector
Limit range for x and y values between 10 & -10.
loop to show user the points they just entered and ask what they would like to enter next
the loop above should continue until the break is triggered
here is my header for CartesianPoints
#ifndef MY_CARTESIAN_POINT_H
#define MY_CARTESIAN_POINT_H
#include <iostream> // cin, cout
#include <sstream> // stringstream
#include <cmath> // sqrt()
#include <limits> // INT_MAX
#include <stdexcept> // out_of_range
using namespace std;
class CartesianPoint
{
public:
CartesianPoint(int x = 1, int y = 1) { SetPoint(x, y); }
int GetX() const { return myX; }
int GetY() const { return myY; }
double GetDistanceTo(CartesianPoint pointTo) const;
string ToString() const;
void SetX(int x) { myX = validateCoordinateValue(x); }
void SetY(int y) { myY = validateCoordinateValue(y); }
void SetPoint(int x, int y) { SetX(x); SetY(y); }
static int GetLimit() { return sharedLimit; }
static void SetLimit(int limit) { sharedLimit = abs(limit); }
private:
int myX;
int myY;
static int sharedLimit;
int validateCoordinateValue(int value) const;
};
int CartesianPoint::sharedLimit = INT_MAX;
double CartesianPoint::GetDistanceTo(CartesianPoint pointTo) const
{
int xDelta = pointTo.myX - myX;
int yDelta = pointTo.myY - myY;
return sqrt((xDelta * xDelta) + (yDelta * yDelta));
}
string CartesianPoint::ToString() const
{
stringstream strOut;
strOut << "(" << myX << ", " << myY << ")";
return strOut.str();
}
int CartesianPoint::validateCoordinateValue(int value) const
{
if((value < -sharedLimit || value > sharedLimit))
{
throw out_of_range( "Parameter (" + to_string(value) + ") must be between "
+ to_string(-sharedLimit) + " and " + to_string(sharedLimit) + ".");
}
return value;
}
#endif
here is my main so far
int main()
{
GreetingScreen(); // just a formatting function ive already created
// while loop that makes will give the option to end the program.
while(/* if myX! =10 and myY!= 10 keep doing this loop */ )
{
// try catch for errors....
try
{
cout << "Move from point" /* (0,0)*/ "to where?" << endl;
cout << "X: " << endl;
cin >> x; //point x
cout << "Y: " << endl;
cin >> y; //point y
catch
{
cerr << "could not do this task";
}
}
} // ending of while loop
} // ending of main
You are on the right path, but there are a few things that I do see a concern with.
In your class I don't see where you are using <iostream> I think you can omit it.
You are using using namespace std. It is preferred to just scope out the namespace std::.
About your constructor:
CartesianPoint(int x = 1, int y = 1) { SetPoint(x, y); }
Here you have two default values, there are two options here:
Declare this constructor as explicit
explicit CartesianPoint( int x = 1, int y = 1 ) { SetPoint( x, y ); }
Declare a default and user defined constructors
CartesianPoint() { SetPoint( x, y ); }
CartesianPoint( int x, int y ) { SetPoint( x, y ); } // by value
CartesianPoint( int& x, int& y ) { SetPoint( x, y ); } // by reference
Note - The third constructor my require overloads for the SetPoint function to accept by reference, however if you continue reading below you will see what I've done with your class.
Personally I think the 2nd choice is the better of the two. If you choose to use a constructor with 2 parameters and both have default values and you are not declaring the constructor as explicit; you will run into trouble.
This is why I preferably choose to use the 2nd option of declaring both a default constructor and a user defined constructor. This gives you the flexibility to do any of the following:
{
CartesianPoint p1; // default constructor called
CartesianPoint p2( 5, 6 ); // user defined constructor called.
int x = 5;
int y = 6;
CartesianPoint p3( x, y ); // another user defined constructor called.
}
Now there is something else with the constructor: you are calling a member function to set the point (x,y) This is not really needed. Class's have a member initializer list; use them! You are also using member functions SetX() and SetY() in your member function SetPoint() the extra calls are not needed.
Personally I would write your class as such:
#ifndef MY_CARTESIAN_POINT_H
#define MY_CARTESIAN_POINT_H
// #include <iostream> // cin, cout
#include <sstream> // stringstream
#include <cmath> // sqrt()
#include <limits> // INT_MAX
#include <stdexcept> // out_of_range
// using namespace std;
class CartesianPoint {
private:
int myX;
int myY;
static int sharedLimit;
public:
CartesianPoint() : myX( 0 ), myY( 0 ) {} // I chose 0, but you can choose any default values for (x,y)
CartesianPoint( int x, int y ) :
myX( validate( x ) ),
myY( validate( y ) ) {
}
CartesianPoint( int& x, int& y ) :
myX( validate( x ) ),
myY( validate( y ) ) {
}
int GetX() const { return myX; }
int GetY() const { return myY; }
// by value
void SetX(int x) { myX = validate(x); }
void SetY(int y) { myY = validate(y); }
void SetPoint(int x, int y) {
myX = validate( x );
myY = validate( y );
}
// by reference
void SetX( int& x ) { myX = validate(x); }
void SetY( int& y ) { myX = validate(y); }
void SetPoint( int& x, int& y ) {
myX = validate( x );
myY = validate( y );
}
double GetDistanceTo(CartesianPoint pointTo) const;
string ToString() const;
static int GetLimit() { return sharedLimit; }
static void SetLimit(int limit) { sharedLimit = abs(limit); }
private:
int validate( int value ) const; // by value
int validate( int& value ) const; // by reference
};
int CartesianPoint::sharedLimit = INT_MAX;
double CartesianPoint::GetDistanceTo(CartesianPoint& pointTo) const {
int xDelta = pointTo.myX - myX;
int yDelta = pointTo.myY - myY;
return sqrt((xDelta * xDelta) + (yDelta * yDelta));
}
std::string CartesianPoint::ToString() const {
std::stringstream strOut;
strOut << "(" << myX << ", " << myY << ")";
return strOut.str();
}
int CartesianPoint::validate(int value) const {
return validate( value );
}
int CartesianPoint::validate( int& value ) const {
if((value < -sharedLimit || value > sharedLimit)) {
std::ostringstream stream;
stream << "Out Of Range: Parameter ("
<< + ToString(value)
<< + ") must be between "
<< + ToString(-sharedLimit)
<< + " and "
<< + ToString(sharedLimit)
<< + '.';
throw stream.str();
}
return value;
}
#endif
main.cpp
#include <iostream>
#include "CartesianPoint.h"
int main() {
try {
std::vector<CartesianPoint> points; // It's already empty
while( condition(s) ) {
// do work
}
} catch( std::string& str ) {
std::cout << str << std::endl;
return -1;
} catch( ... ) {
std::cout << "Caught some other or unknown exception." << std::endl;
return -1;
}
return 0;
}
EDIT - I made a change to the validateCoordinateValue I first changed it's name to just validate for several reasons:
1st: It is a private method to the function and it isn't exposed as part of its public interface.
2nd: It is shorter and easier to type as well as read.
3rd: When using it in the class just as validate() it is already self explanatory of what the function does. Compare the two:
myX = validateCoordinateValue( x );
myX = validate( x );
Then I also added in an overload of the function to accept pass by reference as well. The reference version does the work, the pass by value function just simply returns and calls the reference version.

class member functions for C++ newbie [duplicate]

This question already has answers here:
Visual Studio 2015 “non-standard syntax; use '&' to create a pointer to member”
(3 answers)
Closed 5 years ago.
I'm new with C++ and I'm currently studying for exams, messing around with C++ in VisualStudio and experimenting a bit. Usuall I work with Java.
I wrote a simple class to see how and if things work:
class Point
{
private:
int x;
int y;
public:
Point(int arg1, int arg2)
{
x = arg1;
y = arg2;
}
};
I tried 2 simple member functions for x and y to just double the value stored in the x and y variables.
First I tried this:
void doubleX()
{
x *= 2;
};
void doubleY()
{
y *= 2;
};
Then I tried this:
void doubleX()
{
Point::x = 2 * Point::x;
};
void doubleY()
{
Point::y = 2 * Point2::y;
};
Both are put inside the class definition.
While building through VisualStudio it alwas gives me this error warning:
"Error C3867 'Point::doubleX': non-standard syntax; use '&' to create a pointer to member"
Tried to mess around with adress pointers as well but... I don't really have a clue.
I think I know how pointers basically work, but I have no idea how to use it for my case here.
Any quick solution and explanation to this problem?
Thanks in advance!
EDIT: here's my whole code, problem is in the main now
#include "stdafx.h"
#include <iostream>
using namespace std;
class Point
{
public:
int x;
int y;
Point(int arg1, int arg2)
{
x = arg1;
y = arg2;
}
void doubleX()
{
x *= 2;
};
void doubleY()
{
y *= 2;
};
};
int main()
{
Point p(1,1);
int &x = p.x;
int &y = p.y;
cout << x << "|" << y;
p.doubleX; p.doubleY; //error message here
cout << x << "|" << y;
cin.get();
}
Maybe you didn't declare the member functions inside the class definition? Here is a full working example based on your class:
#include <iostream>
class Point
{
private:
int x;
int y;
public:
Point(int arg1, int arg2)
{
x = arg1;
y = arg2;
}
void doubleX()
{
x *= 2; /* or this->x *= 2; */
}
void doubleY()
{
y *= 2;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
};
int main()
{
Point p(2, 3);
std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl;
p.doubleX();
p.doubleY();
std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl;
return 0;
}
You can put this in a main.cpp file, compile and run it. I tested it with the g++ compiler and it works fine.
The answer given by Valy is correct. But I would like remind you that C++ offers you another choice of declaring and defining methods, that is declaring method inside the class declaration and defining them outside the class declaration. This enables you to easily separate interface and implementation into .h and .cpp files, respectively, as shown below:
Point.h
class Point
{
private:
int x;
int y;
public:
Point(int arg1, int arg2);
void doubleX();
void doubleY();
int getX();
int getY();
};
Point.cpp
#include "Point.h"
Point::Point(int arg1, int arg2)
{
x = arg1;
y = arg2;
}
void Point::doubleX()
{
x *= 2;
}
void Point::doubleY()
{
y *= 2;
}
int Point::getX()
{
return x;
}
int Point::getY()
{
return y;
}
// PointTest.cpp
#include "Point.h"
int main()
{
// Do something with Point here
Point pt(1, 2);
std::cout << "Original: (" << pt.getX() << ", " << pt.getY() << ")" << std::endl;
pt.doubleX();
pt.doubleY();
std::cout << "After being doubled: (" << pt.getX() << ", " << pt.getY() << ")" << std::endl;
return 0;
}
And, how to compile:
g++ -o PointTest PointTest.cpp Point.cpp
Can't comment due to reputation but it seems vc++ outputs the error message you stated if you try to call
Point::doubleX
Here's a live example of the output:
http://rextester.com/ZLCEW66682
You should create an instance of the class and call the function using parens
In your second set of functions
void doubleX()
{
Point2::x = 2 * Point2::x;
};
void doubleY()
{
Point2::y = 2 * Point2::y;
};
If you want them to be member functions of the class Point, Point::y ... this is not how you should access the member data. Only static member variables can be accessed like that. The correct way is
void doubleX()
{
this->x = 2 * this->x;
};
void doubleY()
{
this->y = 2 * this->y;
};
That is using this pointer.

Point2D class objects

This is what I need to do: Add a new data member, string color to your Point2D class and a new getter and setter function for color. Create a Point2D object and set its color. Then create a Point3D color and try to set its color. Is this setColor behavior available for a Point3D class? Why or why not?
This is my code:
#include<iostream>
#include<vector>
using namespace std;
class Point2D
{
friend class SPALops;
protected:
float x;
float y;
protected:
float getX(){
return x;
};
float getY(){
return y;
};
void setX(float xc){
x = xc;
};
void setY(float yc){
y = yc;
};
Point2D(int xcoord, int ycoord){
x = xcoord;
y = ycoord;
};
};
class Point2D : Point2D
{
friend class SPALops;
public:
Point2D(int x, int y) : Point2D(x,y){}
Point2D() : Point2D(0,0){}
float getX(){
return this->Point2D::getX();
};
float getY(){
return this->Point2D::getY();
};
void setX(float x){
this->Point2D::setX(x);
};
void setY(float y){
this->Point2D::setY(y);
};
};
class RectangleImplementation{
friend class SPALops;
protected:
Point2D ll;
Point2D ur;
RectangleImplementation(float llx, float lly, float urx, float ury){
ll.setX(llx); ll.setY(lly);
ur.setX(urx); ur.setY(ury);
}
void setLLx(float x){
ll.setX(x);
};
void setLLy(float y){
ll.setY(y);
};
void setURx(float x){
ur.setX(x);
};
void setURy(float y){
ur.setY(y);
};
float getLLx(){
return ll.getX();
};
float getLLy(){
return ll.getY();
};
float getURx(){
return ur.getX();
};
float getURy(){
return ur.getY();
};
vector<vector<float> > getPointList(){
vector<vector<float> > v(4);
vector<float> llv(2); llv[0] = ll.getX(); llv[1] = ll.getY();
v[0] = llv;
vector<float> luv(2); luv[0] = ll.getX(); luv[1] = ur.getY();
v[1] = luv;
vector<float> ruv(2); ruv[0] = ur.getX(); ruv[1] = ur.getY();
v[2] = ruv;
vector<float> rlv(2); rlv[0] = ur.getX(); rlv[1] = ll.getY();
v[3] = rlv;
return v;
};
void printPointList(){
vector<vector<float>> v = this->getPointList();
cout << "ll = " << v[0][0] << " , " << v[0][1] << endl;
cout << "lu = " << v[1][0] << " , " << v[1][1] << endl;
cout << "ru = " << v[2][0] << " , " << v[2][1] << endl;
cout << "rl = " << v[3][0] << " , " << v[3][1] << endl;
};
};
class Rectangle : RectangleImplementation{
friend class SPALops;
public:
Rectangle(Point2D &p, Point2D &q) : RectangleImplementation(p.getX(), p.getY(), q.getX(), q.getY()){};
Rectangle(float llx, float lly, float urx, float ury): RectangleImplementation(llx,lly,urx,ury){};
float getLLx(){
return this->RectangleImplementation::getLLx();
};
float getLLy(){
return this->RectangleImplementation::getLLy();
};
float getURx(){
return this->RectangleImplementation::getURx();
};
float getURy(){
return this->RectangleImplementation::getURy();
};
void setLLx(float x){
this->RectangleImplementation::setLLx(x);
};
void setLLy(float y){
this->RectangleImplementation::setLLy(y);
};
void setURx(float x){
this->RectangleImplementation::setURx(x);
};
void setURy(float y){
this->RectangleImplementation::setURx(y);
};
void printPointList(){
cout << "In rectangle: " << endl;
cout << "ll = " << ll.getX() << " , " << this->RectangleImplementation::getLLy() << endl;
cout << "ru = " << this->getURx() << " , " << this->RectangleImplementation::getURy() << endl;
};
};
class SPALops{
public:
static bool touches(Rectangle &r, Point2D &p){
vector<vector<float> > v = r.RectangleImplementation::getPointList();
if((v[0][0] == p.getX() and v[0][1] == p.getY()) or
(v[1][0] == p.getX() and v[1][1] == p.getY()) or
(v[2][0] == p.getX() and v[2][1] == p.getY()) or
(v[3][0] == p.getX() and v[3][1] == p.getY()))
{
return true;
}
else
{
return false;
}
};
};
int main(){
Point2D p(10,10);
Point2D q(15,15);
Point2D s(20,20);
Point2D t(10,12);
Rectangle r(p,q);
r.printPointList();
cout << "Do rectangle 'r' and point 'p' touch = " << SPALops::touches(r,p) << endl;
cout << "Do rectangle 'r' and point 's' touch = " << SPALops::touches(r,s) << endl;
cout << "Do rectangle 'r' and point 't' touch = " << SPALops::touches(r,t) << endl;
return 0;
}
It seems I have many errors that stop me from running succesfully. I would appreciate if I get any feedback.
Here are some issues I found in your post:
1) Duplicate classes.
You have a Point2D class first, then a 2nd Point2D class.
Maybe you wanted the 2nd class to be Point3D.
2) Public Inheritance.
Your class Point2D: Point2D is private inheritance.
You may want to use public inheritance:
class Point3D : public Point2D
3) The this-> notation not needed.
When accessing members or functions of the class, access them directly:
int getX() const
{ return x; }
4) Don't duplicate parent functions.
No need to have parent functions repeated in the child class. For example, the child class doesn't need a getX method. This is what inheritance is for -- so you don't need to duplicate methods.
5) Child constructor calls parent constructor.
Your constructor should look something like this:
Point3D(int new_x, int new_y, int new_z)
: Point2D(new_x, new_y),
z(new_z)
{ ; }
Too many other errors to discuss in one post. The above should get you started and give you patterns to apply to other classes.

Function type error c++

I am creating a class to print a point, compare two points to see if they are equal, and to find the distance between two points using separate methods for each. The method for finding the distance between two points is giving me a type error and I don't know why.
#include <iostream>
using namespace std;
class Point
{//in C++ stuff is private by default
public:
Point() { double x = 0; double y = 0; }
Point (double a, double b) { x = a; y = b; }
void print() { cout << "(" << x << "," << y << ")\n" << endl; }
double getX() { return x; };
double getY() { return y; };
bool compare(Point other) { return (x == other.x && y == other.y); }
void setX(double a)
{if (a >= 0)
{x = a;}};
void setY(double b)
{if (b >= 0)
{y = b;}};
double distance(Point point1, Point point2)
{
return(sqrt (pow (point1.getX-point2.getX,2) + pow(point1.getY-point2.getY,2)));
};
private:
double x, y;
};
bool Compare(Point a, Point b) { return (a.getX() == b.getX()) && (b.getY() == a.getY()); }
int main()
{
Point p1(5,1);
Point p2;
p2.setX(2);
p2.setY(5);
p1.print();
p2.print();
p1.getX();
p1.getY();
p2.getX();
p2.getY();
p1.setX(3.5);
p1.setY(9);
p1.print();
p1.compare(p2);
//or p2.equals(p1);
distance(p1, p2);
cout << "This distance b/w p1 & p2 is:" << distance (p2, p1) << endl;
}
You have to call the methods getX and getY by adding () after each name:
return(sqrt(pow(point1.getX()-point2.getX(),2) + pow(point1.getY()-point2.getY(),2)));
Otherwise you will be subtracting pointers to functions, which isn't allowed.
#include <iostream>
#include <math.h>
using namespace std;
class Point
{//in C++ stuff is private by default
public:
Point() { double x = 0; double y = 0; }
Point (double a, double b) { x = a; y = b; }
void print() { cout << "(" << x << "," << y << ")\n" << endl; }
double getX() { return x; };
double getY() { return y; };
bool compare(Point other) { return (x == other.x && y == other.y); }
void setX(double a)
{if (a >= 0)
{x = a;}};
void setY(double b)
{if (b >= 0)
{y = b;}};
static double distance1(Point point1, Point point2)
{
return(sqrt (pow (point1.getX()-point2.getX(),2) + pow(point1.getY()-point2.getY(),2)));
};
private:
double x, y;
};
bool Compare(Point a, Point b) { return (a.getX() == b.getX()) && (b.getY() == a.getY()); }
int main()
{
Point p1(5,1);
Point p2;
p2.setX(2);
p2.setY(5);
p1.print();
p2.print();
p1.getX();
p1.getY();
p2.getX();
p2.getY();
p1.setX(3.5);
p1.setY(9);
p1.print();
p1.compare(p2);
//or p2.equals(p1);
//distance(p1, p2);
cout << "This distance b/w p1 & p2 is:" << Point::distance1 (p2, p1) << endl;
}
Replace distance function from class. In your case std::distance is called. And try do not use using namespace std; in your code at all.