-4

I'm taking an intro to OOP with c++ class and we were introduced to structures. The teacher wants us to create a structure Point that will be the x and y coordinate of a point and then use those to do various things. An example of what she wants would be " dist – this function will receive two Points and calculate and return the distance between the Points." However, I'm getting an error any time I try to pass to points into a function.

For example if I initialize

float dist(Point a);

define

float dist(Point a){
return 1;
}

I don't get an error, but if I say

float dist(Point a, Point b);
float dist(Point a, Point b){
return 1;
}

The error pops me to a different screen and highlights the line

typedef typename _Iterator::iterator_category iterator_category;

Am I not able to pass two structures in that way or am I just misreading her instructions?

Thanks in advance.

Edit: Here is the complete code as requested that doesn't work.

#include <iostream>
#include <cmath>
using namespace std;

struct Point{
    float x;
float y;
}a,b,c;

Point readPt(Point current);
void showPt(Point current);
float distance(Point first, Point second);

int main(){

int test;
cout << "Enter your first point.\n";
        a = readPt(a);
        cout << "Enter your second point.\n";
        b = readPt(b);
        showPt(a);
        showPt(b);
        test = distance(a, b);
return 0;
}


Point readPt(Point current){
char junk;
cin >> junk >> current.x >> junk >> current.y >> junk;
return current;
}

void showPt(Point current){
cout << "(" << current.x << "," << current.y << ")";
}

float distance(Point first, Point second){
return 1;
}
  • 6
    Are you using, `using namespace std;`? If you are, [**don't**](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – NathanOliver Feb 16 '18 at 17:48
  • 4
    "Can I use multiple structures as arguments to a function in c++?" - Yes. Next question please. – Jesper Juhl Feb 16 '18 at 17:52
  • The code you showed is fine; probably the error comes from somewhere else. Post a [minimum complete example](https://stackoverflow.com/help/mcve) that demonstrates the problem. – Matteo Italia Feb 16 '18 at 17:53
  • 2
    Is the error message a secrete? – 273K Feb 16 '18 at 17:54
  • I've added the complete non-working code. Everything in this OOP class has been using namespace std. – Daniel Kunkler Feb 16 '18 at 18:02

1 Answers1

4

Since you are using using namespace std; your compiler complains about the std::distance function and not your float distance(Point first, Point second); function because there is no -std=c++11 flag in your compilation string. Remove the using namespace std; statement and use proper name lookup with std:: instead.

For more information on the subject see this SO post:
Why is “using namespace std” considered bad practice?

Ron
  • 14,674
  • 4
  • 34
  • 47
  • So essentially I've overloaded a function distance that's in the namespace std and it allows me to use mine if there's only one argument but uses theirs if there are two. I changed the function name to dist and it works just fine. It's good to know that namespace std is bad form, I'll assume the teacher is having us use it for ease. – Daniel Kunkler Feb 16 '18 at 18:42
  • 1
    @DanielKunkler You did not overload it. You introduced the entire `std` namespace into the current scope hence the confusion. Avoid that as it is a bad practice. – Ron Feb 16 '18 at 18:43