-
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.
-
February 15th, 2023, 04:41 AM
#2
Re: Errors with my program
You haven't provided the implementation code for Fraction so I can't try to compile the code. Also the implementation code for
Code:
std::istream &operator>>(std:: istream &, Point &);
std:: ostream &operator<<(std:: ostream &, Point);
If you would provide fraction .cpp and Point.cpp so that there's all the parts required to compile, then we can try to compile and advise on the errors etc.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 15th, 2023, 10:45 AM
#3
Re: Errors with my program
I only made the files shown above. Each code area is its own file.
-
February 15th, 2023, 11:33 AM
#4
Re: Errors with my program
In that case that is part of the problem. You have member function declarations but no member functions definitions. eg the Fraction constructor should set the member variables num and den. In Fraction.cpp you'd have something like:
Code:
#include "Fraction.h"
Fraction::Fraction(int32_t n, int32_t d) : num(n), den(d) {}
and similar for the other Fraction member functions.
You also need to define the class Point << and >> functions. Eg in point.cpp for <<
Code:
#include <iostream>
#include "Point.h"
std::ostream& Point::operator<<(std::ostream& os, const Point& p) {
return os << '(' << p.getX() << ',' << p.getY() << ')';
}
You also need a Point constructor and a Point default constructor.
In the .h files where you have a function declaration that ends with a ; you also need to provide a function definition that contains the implementation code for that function.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 15th, 2023, 12:17 PM
#5
Re: Errors with my program
As a starter for the class Fraction, consider this:
Code:
#include <iostream>
#include <numeric>
#include <exception>
class fraction {
public:
fraction(int n, int d) : num(n), den(d) {
reduce();
}
explicit fraction(int i = 0) : num(i), den(1) {}
fraction(const fraction& fr) = default;
fraction(fraction&& fr) = default;
fraction& operator=(const fraction& fr) = default;
fraction& operator=(fraction&& fr) = default;
fraction& operator=(int i) {
num = i;
den = 1;
return *this;
}
fraction& reduce() {
if (den == 0) {
num = 0;
den = 1;
throw std::overflow_error("Denominator zero exception");
}
if (den < 0) {
num *= -1;
den *= -1;
}
if (den > 1)
if (const int g = std::gcd(num, den); g != 1) {
num /= g;
den /= g;
}
return *this;
}
fraction& invert() {
std::swap(num, den);
return *this;
}
fraction& neg() {
num *= -1;
return *this;
}
fraction operator-() {
fraction f(*this);
return f.neg();
}
fraction& operator+=(const fraction& fr) {
const int l = std::lcm(fr.den, den);
num = l / den * num + l / fr.den * fr.num;
den = l;
return reduce();
}
fraction& operator+=(int i) {
return *this += fraction(i);
}
fraction operator+(const fraction& fr) const {
fraction f(*this);
return f += fr;
}
fraction operator+(int i) const {
fraction f(*this);
return f += i;
}
fraction& operator*=(const fraction& fr) {
num *= fr.num;
den *= fr.den;
return reduce();
}
fraction& operator*=(int i) {
return *this *= fraction(i);
}
fraction operator*(const fraction& fr) const {
fraction f(*this);
return f *= fr;
}
fraction operator*(int i) const {
fraction f(*this);
return f *= i;
}
fraction& operator/=(const fraction& fr) {
fraction f(fr);
return *this *= f.invert();
}
fraction& operator/=(int i) {
fraction f(i);
return *this *= f.invert();
}
fraction operator/(const fraction& fr) const {
fraction f(fr);
return f.invert() *= *this;
}
fraction operator/(int i) const {
fraction f(i);
return f.invert() *= *this;
}
fraction& operator-=(const fraction& fr) {
fraction f(fr);
return *this += f.neg();
}
fraction& operator-=(int i) {
fraction f(i);
return *this += f.neg();
}
fraction operator-(const fraction& fr) const {
fraction f(fr);
return f.neg() += *this;
}
fraction operator-(int i) const {
fraction f(i);
return f.neg() += *this;
}
fraction& operator++() { //Prefix
return *this += 1;
}
fraction operator++(int) { // Postfix
fraction f(*this);
++(*this);
return f;
}
fraction& operator--() { //Prefix
return *this -= 1;
}
fraction operator--(int) { // Postfix
fraction f(*this);
--(*this);
return f;
}
bool operator==(const fraction& fr) const {
return (fr.num == num) && (fr.den == den);
}
bool operator!=(const fraction& fr) const {
return !(*this == fr);
}
bool operator>(const fraction& fr) const {
const int l = std::lcm(fr.den, den);
return ((l / den * num) > (l / fr.den * fr.num));
}
bool operator <=(const fraction& fr) const {
return !(*this <= fr);
}
bool operator>=(const fraction& fr) const {
return (*this == fr) || (*this > fr);
}
bool operator<(const fraction& fr) const {
return !(*this >= fr);
}
bool operator==(int i) const {
return *this == fraction(i);
}
bool operator!=(int i) const {
return !(*this == i);
}
bool operator>(int i) const {
return *this > fraction(i);
}
bool operator<=(int i) const {
return !(*this > i);
}
bool operator>=(int i) const {
return *this >= fraction(i);
}
bool operator<(int i) const {
return !(*this >= i);
}
friend std::ostream& operator<<(std::ostream& os, const fraction& fr);
friend std::istream& operator>>(std::istream& is, fraction& fr);
private:
int num = 0;
int den = 1;
};
inline bool operator==(int i, const fraction& fr) {
return fr == i;
}
inline bool operator!=(int i, const fraction& fr) {
return fr != i;
}
inline bool operator>(int i, const fraction& fr) {
return fr < i;
}
inline bool operator>=(int i, const fraction& fr) {
return fr <= i;
}
inline bool operator<(int i, const fraction& fr) {
return fr > i;
}
inline bool operator<=(int i, const fraction& fr) {
return fr >= i;
}
inline fraction operator+(int i, const fraction& fr) {
return fr + i;
}
inline fraction operator*(int i, const fraction& fr) {
return fr * i;
}
inline fraction operator/(int i, const fraction& fr) {
fraction f(fr);
return f.invert() * i;
}
inline fraction operator-(int i, const fraction& fr) {
fraction f(fr);
return f.neg() + i;
}
std::ostream& operator<<(std::ostream& os, const fraction& fr) {
if ((abs(fr.num) > fr.den) && (fr.den > 1))
os << fr.num / fr.den << "+" << fraction(abs(fr.num) % fr.den, fr.den);
else {
os << fr.num;
if (fr.den != 1)
os << "/" << fr.den;
}
return os;
}
std::istream& operator>>(std::istream& is, fraction& fr) {
auto getden = [&]() {
is >> fr.den;
if (fr.den == 0) {
is.setstate(std::ios::failbit);
fr = 0;
}
};
int n = 0;
fr = 0;
is >> n;
if (auto ch = is.peek(); (ch == '+') || (ch == '/')) {
is.get();
if (ch == '+') {
is >> fr.num;
if (is.peek() == '/') {
is.get();
getden();
if (is.good())
fr = (n < 0) ? n - fr : n + fr;
}
} else {
fr.num = n;
getden();
}
} else
fr = n;
fr.reduce();
return is;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
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
|