|
-
February 14th, 2023, 07:36 PM
#1
Errors with my program
Hello, I am a Computer Science Student and I am working on a program that is to find the closest pair of points on a plane. When I compile the program, I receive several different errors:
error: no matching function for call to 'fastClosestPair(Point&, uint32_t&, uint32_t&, uint32_t&)'
error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'Point')
error: no match for 'operator[ ]' (operand types are 'Fraction [100000]' and 'Fraction')
error: expected initializer before '&' token
error: expected ')' before ' , ' token
Fraction.h
Code:
#include <cstdint>
#ifndef _FRACTION_H
#define _FRACTION_H
#include <iostream>
class Fraction {
public:
Fraction(int32_t n=0,int32_t d=1);
~Fraction() = default;
Fraction &operator=(Fraction rhs);
Fraction operator+(Fraction rhs) ;
Fraction operator-(Fraction rhs) ;
Fraction operator*(Fraction rhs) ;
Fraction operator/(Fraction rhs) ;
bool operator==(Fraction rhs) ;
bool operator!=(Fraction rhs) ;
bool operator<=(Fraction rhs) ;
bool operator>=(Fraction rhs) ;
bool operator<(Fraction rhs) ;
bool operator>(Fraction rhs) ;
[[nodiscard]] int32_t getNum() const { return num; }
[[nodiscard]] int32_t getDen() const { return den; }
private:
int
num,
den;
};
std::istream &operator>>(std::istream &,Fraction &);
std::ostream &operator<<(std::ostream &,Fraction);
#endif
Point.h
Code:
#include <cstdint>
#ifndef _POINT_H
#define _POINT_H
#include <iostream>
#include "Fraction.h"
class Point {
public:
Point();
Point &operator=(Point rhs) {
x = rhs.x;
y = rhs.y;
return *this;
}
Fraction dist(Point rhs) { return (x - rhs.x) * (x-rhs.x) + (y-rhs.y)*(y-rhs.y); }
Fraction square(Point rhs) { return ((x - rhs.x) * (x - rhs.x)) + ((y - rhs.y) * (y - rhs.y)); }
uint64_t getX() { return x; }
uint64_t getY() { return y; }
Fraction(getX(), getY());
~Point();
private:
uint64_t x;
uint64_t y;
}
std::istream &operator>>(std::istream &, Point &);
std::ostream &operator<<(std::ostream &, Point);
Fraction naiveClosestPair(Point pList[], uint32_t nPoints, uint32_t &besti, uint32_t &bestj);
Fraction fastClosestPair(Point pList[], uint32_t nPoints, uint32_t &besti, uint32_t &bestj);
Fraction fastClosestPairRec(Point pList[], uint32_t first, uint32_t last, uint32_t &besti, uint32_t &bestj);
#endif
project1.cpp
Code:
#include <iostream>
#include <cmath>
#include "Fraction.h"
#include "Point.h"
using namespace std;
const uint32_t
MAX_POINTS = 100000;
Fraction order[MAX_POINTS];
Fraction tempOrder[MAX_POINTS];
//=============================================================================
// int compareX(const void *a,const void *b)
// Compare points by x-coordinates, break ties by y-coordinates
//
// Parameters
// a - pointer to a Point object
// b - pointer to a Point object
//
// Returns
// -1 if a < b
// 0 if a == b
// 1 if a > b
//
int compareX(const void *a,const void *b) {
auto
*pa = (Point *)a,
*pb = (Point *)b;
Fraction
d = pa->getX() - pb->getX(); // difference in x-coordinates
// if x-coordinates are different, return 1 or -1
if (d < 0)
return -1;
if (d > 0)
return 1;
// if we get here, x-coordinates are the same, do the same testing
// with the y-coordinates
d = pa->getY() - pb->getY();
if (d < 0)
return -1;
if (d > 0)
return 1;
// if we get here, the points are the same
return 0;
}
//=============================================================================
// void sortPointsX(Point *pList,uint32_t nPoints)
// sort an array of Point objects by x-coordinate
//
// Parameters
// pList - an array of Point objects
// nPoints - the number of Point objects in the pList array
//
// Note:
// - this just uses the C library's qsort() function. This requires a callback
// function to compare elements; that's the purpose of the compareX() function
// above
//
void sortPointsX(Point *pList,uint32_t nPoints) {
qsort(pList,nPoints,sizeof(Point),compareX);
}
const Fraction infinity(1,0);
// Algorithm 1
Fraction naiveClosestPair(Point pList[], uint32_t nPoints, uint32_t p1, uint32_t p2)
{
Fraction best = infinity;
uint32_t besti, bestj;
for (int i = 0; i < nPoints - 2;)
{
for (int j = i + 1; j < nPoints - 1;)
{
Fraction d = (pList[i].getX() - pList[j].getX()) + (pList[i].getY() - pList[j].getY());
if (d < best)
{
Fraction best = d;
uint32_t besti = i;
uint32_t bestj = j;
}
}
}
return best;
return besti;
return bestj;
}
// Algorithm 3 with 4 and 5
Fraction fastClosestPairRec(Point pList[], uint32_t first, uint32_t last)
{
Fraction best, d, d1, d2;
uint32_t i1, i2, j1, j2, besti, bestj;
if (first == last)
{
order[first] = first;
return infinity, first, first;
}
uint32_t mid = (first + last) / 2;
d1 = fastClosestPairRec(pList[MAX_POINTS], first, mid, i1, j1);
d2 = fastClosestPairRec(pList[MAX_POINTS], first, mid, i2, j2);
if (d2 < d1)
{
best = d2;
besti = i2;
bestj = j2;
} else {
best = d1;
besti = i1;
bestj = j1;
}
Fraction left = first;
Fraction right = mid + 1;
Fraction k = first;
while (left <= mid && right <= last)
{
if (pList[order[right]].getY() < pList[order[left]].getY() || (pList[order[right]].getY() == pList[order[left]].getY() && pList[order[right]].getX() < pList[order[left]].getX()))
{
tempOrder[k] = order[right];
right = right + 1;
} else {
tempOrder[k] = order[left];
left = left + 1;
}
k = k + 1;
}
while (left <= mid)
{
tempOrder[k] = order[left];
left = left + 1;
k = k + 1;
}
while (right <= last)
{
tempOrder[k] = order[right];
right = right + 1;
k = k + 1;
}
left = first;
for (k = first; k < last; k + 1)
{
order[k] = tempOrder[k];
if (pList[order[k]].getX() - pList[mid].getX() <= best)
{
tempOrder[left] = order[k];
left = left + 1;
}
}
for (k = first; k < left - 1; k + 1)
{
for (right = k + 1; right < left - 1; right + 1)
{
if (pList[tempOrder[k]].getY() - pList[tempOrder[right]].getY() > best)
{
break;
}
d = pList[tempOrder[k]].dist(pList[tempOrder[right]]);
if (d < best)
{
best = d;
besti = tempOrder[k];
bestj = tempOrder[right];
}
}
}
return best;
}
//Algorithm 2
Fraction fastClosestPair(Point pList[], uint32_t nPoints, uint32_t p1, uint32_t p2)
{
Fraction d;
uint32_t besti, bestj;
d, besti, bestj = fastClosestPairRec(pList[MAX_POINTS], 0, nPoints - 1);
return d, besti, bestj;
}
//=============================================================================
// int main(int32_t argc,char *argv[])
// the main function
//
// Parameters
// argc - number of elements given on the command line. Should be 2.
// argv - array of C strings holding the contents of the command line
//
// Returns
// 0 if the program runs successfully, 1 if not
//
int main(int32_t argc,char *argv[]) {
Point
pList[MAX_POINTS];
uint32_t
nPoints,
p1,p2;
Fraction
minDistNaive,
minDistFast;
double
d;
// make sure there's an option (-n, -f or -b) given on the command line
if (argc < 2 || argv[1][0] != '-') {
cout << "Usage error" << endl;
return 1;
}
// read the points
cin >> nPoints;
for (uint32_t i = 0; i < nPoints; i++) {
cin >> pList[i]; }
// -n = use the brute force / naive method
if (argv[1][1] == 'n') {
minDistNaive = naiveClosestPair(pList[MAX_POINTS], nPoints, p1, p2);
cout << "Minimum distance (squared): " << minDistNaive << endl;
cout << "points: " << pList[p1] << ' ' << pList[p2] << endl;
d = sqrt(minDistNaive.getNum()) / sqrt(minDistNaive.getDen());
cout << "Distance: " << d << endl;
// -f = use the fast / divide and conquer method
} else if (argv[1][1] == 'f') {
minDistFast = fastClosestPair(pList[MAX_POINTS], nPoints, p1, p2);
cout << "Minimum distance (squared): " << minDistFast << endl;
cout << "points: " << pList[p1] << ' ' << pList[p2] << endl;
d = sqrt(minDistFast.getNum()) / sqrt(minDistFast.getDen());
cout << "Distance: " << d << endl;
// -b = use both methods to compare answers
} else if (argv[1][1] == 'b') {
minDistNaive = naiveClosestPair(pList[MAX_POINTS], nPoints, p1, p2);
cout << "Brute force:" << endl;
cout << "Minimum distance (squared): " << minDistNaive << endl;
cout << "points: " << pList[p1] << ' ' << pList[p2] << endl;
d = (sqrt(minDistNaive.getNum() / (sqrt(minDistNaive.getDen()))));
cout << "Distance: " << d << endl;
cout << "\nFast:" << endl;
minDistFast = fastClosestPair(pList[MAX_POINTS], nPoints, p1, p2);
cout << "Minimum distance (squared): " << minDistFast << endl;
cout << "points: " << pList[p1] << ' ' << pList[p2] << endl;
d = sqrt(minDistFast.getNum()) / sqrt(minDistFast.getDen());
cout << "Distance: " << d << endl;
} else {
cout << "Usage error" << endl;
return 1;
}
return 0;
}
If anyone could let me know how I can resolve these errors that would be greatly appreciated.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|