Function type error c++ - 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.

Related

Count how many times class member was printed

I need to count how many times class members were printed using function Print which is inspector. Constructor should set private elements of class.
#include <cmath>
#include <iostream>
class Vector3d {
double x, y, z;
mutable int count = 0;
public:
Vector3d();
Vector3d(double x, double y, double z);
void Print() const;
int GetCount() const;
};
Vector3d::Vector3d() {
count = 0;
}
Vector3d::Vector3d(double x, double y, double z) {
count = 0;
Vector3d::x = x;
Vector3d::y = y;
Vector3d::z = z;
}
void Vector3d::Print() const {
count++;
std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
return count;
}
int main() {
Vector3d v1(1, 2, 3);
v1.Print();v1.Print();v1.Print();
Vector3d v2(v1);
v2.Print();v2.Print();
std::cout << v2.GetCount();
return 0;
}
I used mutable int to enable changing element of const function. For v1.GetCount() I get output 3, which is correct. However, for v2.GetCount() I get output 5, which is wrong (correct is 2).
Could you help me to fix this? Where am I making mistake?
You need to overload copy constructor and copy assign operator for Vector3d class.
Now you are copying state of count field into v2 object, therefore it starts from 3 not from 0.
#include <cmath>
#include <iostream>
class Vector3d {
double x, y, z;
mutable int count = 0;
public:
Vector3d(double x, double y, double z);
Vector3d(const Vector3d&);
Vector3d& operator=(const Vector3d&);
Vector3d(Vector3d&&) = delete;
Vector3d& operator=(Vector3d&&) = delete;
void Print() const;
int GetCount() const;
};
Vector3d::Vector3d(double x, double y, double z) {
Vector3d::x = x;
Vector3d::y = y;
Vector3d::z = z;
}
Vector3d::Vector3d(const Vector3d& that)
: Vector3d(that.x, that.y, that.z)
{
}
Vector3d& Vector3d::operator=(const Vector3d& that)
{
x = that.x;
y = that.y;
z = that.z;
return *this;
}
void Vector3d::Print() const {
count++;
std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
return count;
}
int main() {
Vector3d v1(1, 2, 3);
v1.Print();v1.Print();v1.Print();
Vector3d v2(v1);
v2.Print();v2.Print();
std::cout << v2.GetCount();
return 0;
}
UPDATE:
Someone mentioned that explicitly deleted move ctor and operator is not ok, I understand that, but for me it is not clear should we move counter to other instance or not. Therefore here possible implementation:
#include <cmath>
#include <iostream>
class Vector3d {
double x, y, z;
mutable int count = 0;
public:
Vector3d(double x, double y, double z);
Vector3d(const Vector3d&);
Vector3d(Vector3d&&);
Vector3d& operator=(Vector3d);
void Print() const;
int GetCount() const;
private:
void swap(Vector3d&);
};
Vector3d::Vector3d(double x, double y, double z) {
Vector3d::x = x;
Vector3d::y = y;
Vector3d::z = z;
}
Vector3d::Vector3d(const Vector3d& that)
: Vector3d(that.x, that.y, that.z)
{
}
Vector3d::Vector3d(Vector3d&& that)
: Vector3d(that.x, that.y, that.z)
{
count = that.count;
}
Vector3d& Vector3d::operator=(Vector3d that)
{
swap(that);
return *this;
}
void Vector3d::swap(Vector3d& that)
{
std::swap(x, that.x);
std::swap(y, that.y);
std::swap(z, that.z);
std::swap(count, that.count);
}
void Vector3d::Print() const {
count++;
std::cout << "{" << x << "," << y << "," << z << "}";
}
int Vector3d::GetCount() const {
return count;
}
int main() {
Vector3d v1(1, 2, 3);
v1.Print();v1.Print();v1.Print();
Vector3d v2 = std::move(v1);
v2.Print();v2.Print();
std::cout << v2.GetCount();
return 0;
}
But these more for commenters than for question author.

Return object from class member function in C++

The task is in the above code to write member function that calculate new point, which is amount of two other points. And i dont know how to return object or what should i do. Here is the code, and the function is marked with three !!!. The function must return something, i cant make it void because reference to void is unallowed.
class point {
private:
float x;
float y;
public:
point();
point(float xcoord, float ycoord);
void print();
float dist(point p1, point p2);
!!! float &add(point p1, point p2);
float &X();
float &Y();
~point();
};
float & point::X() { return x; }
float & point::Y() { return y; }
point::point() {
cout << "Creating POINT (0,0)" << endl;
x = y = 0.0;
}
point::point(float xcoord, float ycoord) {
cout << "Creating POINt (" << xcoord << "," << ycoord << ")" << endl;
x = xcoord;
y = ycoord;
}
void point::print() {
cout << "POINT (" << x << "," << y << ")";
}
float point::dist(point p1, point p2) {
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
}
!!!// float & point::add(point p1, point p2) {
point z;
z.X() = p1.X() + p2.X();
z.Y() = p1.Y() + p2.Y();
z.print();
}
point::~point() {
cout << "Deleting ";
print();
cout << endl;
}
int main() {
point a(3, 4), b(10, 4);
cout << "Distance between"; a.print();
cout << " and "; b.print();
cout << " is " << a.dist(a, b) << endl;
}
i make it ! here is what must be add function
//prototype
point &add(point& p1, point& p2);
//function itself
point & point::add(point& p1, point& p2) {
point z;
z.x = p1.X() + p2.X();
z.y = p1.Y() + p2.Y();
z.print();
return z;
}
Many thanks to ForceBru!! and all of you
What to do
You can return a point as well:
point point::add(const point& p1, const point& p2) {
point result;
result.x = p1.x + p2.x;
result.y = p1.y + p2.y;
return result;
}
Note that there's no need to use X() and Y() functions here since this method already has access to the private members.
It's also possible to do an operator overload
/*friend*/ point operator+ (const point& one, const point& two) {
// the code is the same
}
How to use it
int main() {
point one(2,5), two(3,6), three;
three.add(one, two);
std::cout << "Added " << one.print() << " and " << two.print();
std::cout << " and got " << three.print() << std::endl;
return 0;
}
Edit: as it was said in the comments, you shouldn't return a reference to an object created inside your add function since in such a situation you're allowed to return references to class members and to static variables only.
You can use Operator overloading here:
point point::operator+(const point & obj) {
point obj3;
obj3.x = this->x + obj.x;
return obj3;
}
returning object with Addition of two points.
for simple example :
class Addition {
int a;
public:
void SetValue(int x);
int GetValue();
Addition operator+(const Addition & obj1);
};
void Addition::SetValue(int x)
{
a = x;
}
int Addition::GetValue()
{
return a;
}
Addition Addition::operator+(const Addition &obj1)
{
Addition obj3;
obj3.a = this->a + obj1.a;
return obj3;
}
int _tmain(int argc, _TCHAR* argv[])
{
Addition obj1;
int Temp;
std::cout<<"Enter Value for First Object : "<<std::endl;
std::cin>>Temp;
obj1.SetValue(Temp);
Addition obj2;
std::cout<<"Enter Value for Second Object : "<<std::endl;
std::cin>>Temp;
obj2.SetValue(Temp);
Addition obj3;
obj3 = obj1 + obj2;
std::cout<<"Addition of point is "<<obj3.GetValue()<<std::endl;
return 0;
}

c++ how to conserve vector items and double-precision

I'm trying to fill a vector of an object Point 3D. My app read a csv file to load the vector by the three cordinate x, y, z. I use the type float.
This is my code.
main.cpp
int main(int argc, char** argv) {
char *theFileName = "file.csv"; //[100];
vector<Point> v = getPointCloud(theFileName);
for (int i = 0; i < v.size(); ++i) {
v.at(i).print(cout);
}
}
getPointCloud
vector<Point> getPointCloud(char *fileName) {
string line;
string token;
vector<Point> v;
double tab[3];
ifstream file(fileName);
if (file.is_open()) {
while (getline(file, line)) {
int cpt = 0;
stringstream stream(line);
while (getline(stream, token, ',')) {
tab[cpt] = ::atof(token.c_str());
cpt++;
}
Point p(tab[0], tab[1], tab[2]);
p.print(cout); <-- the display works
p.setColor(255, 0, 0);
v.push_back(p);
}
file.close();
} else {
cout << "Unable to open " << fileName << '\n';
exit(0);
}
return v;
}
I have two problems:
1 - when I try to display points in the main method, I found that the three coordinates are null ( == 0) but in the displaying in the getPointCloud method works very well.
2 - Can someone give a simple method to conserve my coordinates without loss precision after mathematical operations. I have searched in the net but I don't understand haw to solve it. I'm newbie with c++.
Point.h
#ifndef POINT_H
#define POINT_H
#include <math.h>
#include <iostream>
class Point {
protected:
float x;
float y;
float z;
// color RGB
float r;
float g;
float b;
public:
// Constructors
Point();
// Point(const Point& orig);
Point(std::ostream &strm);
Point(float x, float y, float z);
Point(const Point& orig);
virtual ~Point();
//getters
float getX() const {
return this->x;
}
float getY() const {
return this->y;
}
float getZ() const {
return this->z;
}
float getR() const {
return this->r;
}
float getG() const {
return this->g;
}
float getB() const {
return this->b;
}
//setters
void setX(float x) {
this->x = x;
}
void setY(float y) {
this->y = y;
}
void setZ(float z) {
this->z = z;
}
void setR(float r) {
this->r = r;
}
void setG(float g) {
this->g = g;
}
void setB(float b) {
this->b = b;
}
void setColor(float r, float g, float b) {
this->r = r;
this->g = g;
this->b = b;
}
/**
* Print the point
* #param strm
*/
void print(std::ostream &strm);
//Other methods
float dist2D(Point &other);
float dist3D(Point &other);
Point swap(Point p);
// Point operator-(const Point &other) const;
};
#endif /* POINT_H */
Point.cpp
#include <iostream>
#include <math.h>
#include <ostream>
using namespace std;
#include "Point.h"
Point::Point(const Point& orig) {
}
Point::Point(ostream &strm) {
strm << "Type the abscissa: ", cin >> this->x;
strm << "Type the ordinate: ", cin >> this->y;
strm << "Type the applicate: ", cin >> this->z;
}
Point::Point(float x, float y, float z) : x(x), y(y), z(z) {
// The default point color is blue
this->r = 0;
this->g = 0;
this->b = 255;
}
/**
* Destructor
*/
Point::~Point() {
}
//Other methods
float Point::dist2D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
return sqrt(xd * xd + yd * yd);
}
float Point::dist3D(Point &other) {
float xd = x - other.x;
float yd = y - other.y;
float zd = z - other.z;
return sqrt(xd * xd + yd * yd + zd * zd);
}
Point Point::swap(Point p) {
Point aux(x, y, z);
x = p.x;
y = p.y;
z = p.z;
return aux;
}
//Point Point::operator-(const Point &other) const {
// return Point(other.getX() - this->x, other.getY() - this->y, other.getZ() - this->z);
//}
void Point::print(ostream &strm) {
strm << "Point(" << this->x << "," << y << "," << z << ")" << endl;
}
Thanks in advance.
Point::Point(const Point& orig) {
}
is incorrect.
It does not copy data from orig to *this
Please copy each of the member in this constructor.
This would look like this:
Point::Point(const Point& orig) {
x = orig.x ;
y = orig.y ;
x = orig.z ;
r = orig.r ;
g = orig.g ;
b = orig.b ;
}

Order: An Analysis on Point Sorting

So I've made for myself a point printing class, that is supposed to have the user enter in 2-tuples; that is, x and y, that then prints them back to the user in ^order,^ where order means p1=(x,y)
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
class Point2D {
public:
Point2D();
Point2D(double a, double b);
double getx();
double gety();
void setx(double a);
void sety(double b);
virtual void print();
virtual void print(int a);
double angle();
private:
double x;
double y;
};
bool operator<( Point2D a , Point2D b );
int main() {
double my_x=-999;
double my_y=-999;
string my_color;
double my_weight;
vector<Point2D*> points;
cout << "Welcome to Point Printer! Please insert the x-and y-coordinates for your points and I will print them in sorted order! Just one rule, the point (0,0) is reserved as the terminating point, so when you are done enter (0,0).\n";
while(true)
{
cout << "x = ";
cin>>my_x;
cout << "y = ";
cin>>my_y;
if((my_x == 0)&&(my_y==0))
{
break;
}
points.push_back(new Point2D(my_x, my_y));
}
sort(points.begin(), points.end());
cout << "\n\n";
cout << "Your points are\n\n";
for(int i=0;i<points.size();i++)
{
cout<<i+1<<": ";
(*points[i]).print(); cout<<endl; // this is the printing gadget
}
for(int i=0; i<points.size(); i++)
{
delete points[i];
}
cout << endl << endl;
return 0;
}
double Point2D::angle()
{
double Angle = atan2(y,x);
if(Angle < 0)
{
return Angle + 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679;
}
return Angle;
}
bool operator< (Point2D a, Point2D b)
{
if (a.getx()*a.getx()+a.gety()*a.gety() < b.getx()*b.getx()+b.gety()*b.gety())
{
return true;
}
else if (a.getx()*a.getx()+a.gety()*a.gety() > b.getx()*b.getx()+b.gety()*b.gety())
{
return false;
}
if (a.getx()*a.getx()+a.gety()*a.gety() ==b.getx()*b.getx()+b.gety()*b.gety())
{
if (a.angle() < b.angle())
{
return true;
}
else if (a.angle() > b.angle())
{
return false;
}
}
return true;
}
Point2D::Point2D() { x = 0; y = 0; return;}
Point2D::Point2D(double a, double b) { x = a; y = b; return;}
double Point2D::getx() { return x;}
double Point2D::gety() { return y;}
void Point2D::setx(double a) { x = a; return; }
void Point2D::sety(double b) { y = b; return; }
void Point2D::print() {
cout<<"("<<x<<","<<y<<")";
return;
}
void Point2D::print(int a) {
print(); cout<<endl;
}
What I'm having trouble with is either one of the following:
sort
angle()
operator<(Point2D a, Point2D b)
Something different entirely...
In particular, the following points:
x = 1
y = 2
x = 2
y = 3
x = 1.1
y = 2.2
x = -10
y = 10
x = -5
y = -3
x = -5
y = 3
x = 5
y = -3
x = 5
y = 3
x = 0
y = 0
are not sorted in the correct order.
Any help would be much appreciated. Thank you.
The problem (or one of them) is the final statement in your comparison function.
return true;
Look at this block:
if (a.getx()*a.getx()+a.gety()*a.gety() ==b.getx()*b.getx()+b.gety()*b.gety())
{
if (a.angle() < b.angle())
{
return true;
}
else if (a.angle() > b.angle())
{
return false;
}
}
First of all, if we've gotten to this point, we've determined that the (x*x + y*y) calculations for both a and b are equal. Now let's assume that the angle is also equal. What happens? The first test fails because a.angle() is not less than b.angle(). Then the second test fails because a.angle() is not greater than b.angle(). Then you return true. In other words, you're saying that it is true that a is less than b, even though by all rights, they should be considered equal, and so you should return false. Instead of multiple tests on the angle, you can just return a.angle() < b.angle();, and that should do the trick. With some additional simplifications, your function should look something like this:
bool operator<(Point2d a, Point2d b)
{
double A = a.getx()*a.getx()+a.gety()*a.gety();
double B = b.getx()*b.getx()+b.gety()*b.gety();
if (A < B) return true;
if (A > B) return false;
return a.angle() < b.angle();
}
The problem is probably that you are storing and sorting pointers, not objects. The points will be compared not with your operator but their addresses. Try change points to vector<Point2d>
First of all just use (if your are just planning to sort 2D points) :
(Edit : See Benjamin Lindley comments below.)
bool operator < ( Point2D a, Point2D b)
{
return a.getx() < b.getx() ||
(a.getx()==b.getx() && a.gety()< b.gety() );
}
Another thing if use use std::cout in operator < ( Point2D a, Point2D b), you will notice it won't be called anytime.
The reason is this:
vector<Point2D*> points; // Vector of Point2D*
but bool operator< (Point2D a, Point2D b) is used for comparision.
Suggested Fixes:
vector<Point2D> points;
points.push_back(Point2D(my_x, my_y));
And accordingly, wherever applicable.
Also you can't define anything like
bool operator<(const Point2D* a, const Point2D* b)
Because of this:
C++03 standard, ยง13.5 [over.oper] p6:
An operator function shall either be a non-static member function or
be a non-member function and have at least one parameter whose type is
a class, a reference to a class, an enumeration, or a reference to an
enumeration.

Undefined reference to my classes? C++ Beginner

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.