Help with Cartesian Class
Apr 6, 2014 at 11:17am UTC
I am trying to make a cartesian class which allows the user to input two coordinates, displays the coordinates, and uses memberwise assignment to assign the values of the first object to the second object. When I try to run my program I get the following errors
prog.cpp: In function ‘int main()’:
prog.cpp:47:16: error: ‘class Cartesian’ has no member named ‘a’
cin >> coord1.a;
^
prog.cpp:49:16: error: ‘class Cartesian’ has no member named ‘b’
cin >> coord1.b;
^
prog.cpp:51:16: error: ‘class Cartesian’ has no member named ‘a’
cin >> coord2.a;
^
prog.cpp:53:16: error: ‘class Cartesian’ has no member named ‘b’
cin >> coord2.b;
^
How can I fix this? Also, I'm a little confused as to how to go about writing the code for the memberwise assignment. Any suggestions?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;
class Cartesian
{
private :
double x;
double y;
public :
Cartesian( double = 0, double = 0);
friend istream& operator >>(istream&, Cartesian&);
friend ostream& operator <<(ostream&, const Cartesian&);
};
Cartesian::Cartesian(double a, double b)
{
x=a;
y=b;
}
istream& operator >>( istream& in, Cartesian& num)
{
double a, b;
in >> a;
in >> b;
num.x= a;
num.y= b;
return in;
}
ostream& operator <<( ostream& out, const Cartesian& num)
{
cout << "(" << num.x << ", " << num.y << ")" << endl;
return out;
}
int main()
{
Cartesian coord1, coord2;
cout << "Please enter the first x-coordinate: " ;
cin >> coord1.a;
cout << "Please enter the first y-coordinate: " ;
cin >> coord1.b;
cout << "Please enter the second x-coordinate: " ;
cin >> coord2.a;
cout << "Please enter the second y-coordinate: " ;
cin >> coord2.b;
cout << coord1;
cout << coord2;
return 0;
}
}
Apr 6, 2014 at 11:24am UTC
If you
had not created these members, how would you access them?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;
class Cartesian
{
private :
double x;
double y;
public :
Cartesian( double = 0, double = 0);
friend istream& operator >>(istream&, Cartesian&);
friend ostream& operator <<(ostream&, const Cartesian&);
};
x and y, no a and b
Last edited on Apr 6, 2014 at 11:32am UTC
Apr 6, 2014 at 11:29am UTC
How do I create them as members?
Apr 6, 2014 at 11:31am UTC
Like x and y, but then you'll have to change your methods (functions inside the class)
Apr 6, 2014 at 11:42am UTC
Okay so I changed my code and it runs but it doesn't ask for user input. What's wrong with it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#include <iostream>
#include <istream>
#include <ostream>
using namespace std;
class Cartesian
{
private :
double x;
double y;
public :
Cartesian( double = 0, double = 0);
friend istream& operator >>(istream&, Cartesian&);
friend ostream& operator <<(ostream&, const Cartesian&);
}
;
Cartesian::Cartesian(double a, double b)
{
x=a;
y=b;
}
istream& operator >>( istream& in, Cartesian& num)
{
in >> num.x;
in >> num.y;
return in;
}
ostream& operator <<( ostream& out, const Cartesian& num)
{
cout << "(" << num.x << ", " << num.y << ")" << endl;
return out;
}
int main()
{
Cartesian coord1, coord2;
cout << "Please enter the first coordinates in the form x y: " ;
cin >> coord1;
cout << "Please enter the second coordinates in the form x y: " ;
cin >> coord2;
cout << coord1;
cout << coord2;
return 0;
}
Last edited on Apr 6, 2014 at 7:49pm UTC
Topic archived. No new replies allowed.