Problem with classes

So i have this Program:


#include <iomanip>
#include <cmath>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

class Pair {

public:
int first;
int second;
Pair ();
Pair (int,int);
double distanceTo (Pair);
void print (double);

};

Pair::Pair(int a, int b)
{
first = a;
second = b;
}
double Pair::distanceTo (Pair b)
{
double distance;
double xcord = (this.first - b.first);
double ycord = (this.second - b.second);
distance = sqrt(pow(xcord,2) + pow(ycord,2));

return distance;
}
void print(double a)
{
cout << "the distance to a is " << a;
return;
}

int main()
{
Pair N1(12,6);
Pair N2(4,9);
int xcor;
int ycor;


double disN1;
double disN2;
cout << "please enter a pair of numbers";

cin >> xcor >> ycor;
Pair N3(xcor,ycor);
disN1 = N3.distanceTo(N1);
disN2 = N3.distanceTo(N2);
print(disN1);
print(disN2);


return 0;
}


that keeps getting the error 2228 left of .first must have class/struct/union

and i have no clue what i'm doing wrong, it show it like this in my book but never does an example like this, i'm in c++ and any help would be nice.
The first error is missing code tags.
if you dont know what code tags are you click the '<>' button under 'Format:' and you put your program inside them.
Last edited on
And please indent your code.
The first error is missing code tags.
LOL

please read this OP http://cplusplus.com/articles/firedraco1/
"this" is a pointer, but you are using it as "this.<something>". It should be this-><something>.

Second, you've declared a default constructor for Pair but never implemented it.

Third, you should use initializer lists in the Pair constructor instead of assignment, if you've learned
initializer lists.

Fourth, x * x is strongly preferred to pow( x, 2 ), only because pow() is orders of magnitude slower
because it is more general.

Fifth, distanceTo should take its parameter by const reference instead of by value, if you've learned
about references.

Sixth, print() has no business being a member of Pair.

Seventh, prefer to declare variables at the point at which they are needed, and no earlier. Eg, do
this: double disN1 = N3.distanceTo(N1); and not

1
2
3
4
5
int main() {
    double disN1;
    // 10 lines of code here
    disN1 = <some number>;
}

Thanks to that last guy a bunch and i do indent my code just so you know but when i copy pasted it unindented them... and i had to put print into the class because thats how the teacher wanted it unfortunatly
Topic archived. No new replies allowed.