How can I display my object of my class?
Apr 19, 2017 at 11:26am UTC
Hello,
Been working on a problem using a class and friend function to convert polar coordinates into polar ones. I just cannot see why I cannot display the newly calculated x and y values after calculation from my friend function.
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
#include <iostream>
#include <cmath>
using namespace std;
class Coord
{
friend Coord convPol(double , double );
private :
double xval;
double yval;
public :
Coord(double = 0, double = 0);
void display();
};
Coord::Coord(double x, double y)
{
xval = x;
yval = y;
}
void Coord::display()
{
cout << "The x co-ordinate is " << xval << endl;
cout << "The y co-ordinate is " << yval << endl;
return ;
}
Coord convPol(double r, double theta)
{
Coord a;
a.xval = r * sin(theta);
a.yval = r * cos(theta);
return a;
}
int main()
{
double r = 0;
double theta = 0;
cout << "This program will convert polar co-ordinates to x and y co-ordinates" << endl;
cout << "Enter your values for r and theta" << endl;
cin >> r;
cin >> theta;
Coord a (convPol(r,theta));
cout << "The values for x and y are " << a.display();
return 0;
}
Apr 19, 2017 at 2:22pm UTC
Coord::display is a void function, so at line 57 you're trying to cout a void.
Put a.display() on its own line (not part of a cout statement).
Last edited on Apr 19, 2017 at 2:24pm UTC
Apr 19, 2017 at 3:59pm UTC
Hi ,
At first, you don't need return in the
void Coord::display()
. Void won't return any value.
One should use return only if the type of fucntion is returning something. like
int add(int x , int y){return x+y;}
And in the main() , again No need of
cout << "The values for x and y are " << a.display();
Calling display() with object is enough. In your case:
1 2
Coord a (convPol(r,theta));
a.display();
will do the task.
Last edited on Apr 19, 2017 at 4:02pm UTC
Apr 19, 2017 at 9:10pm UTC
Hello,
Thanks again! All working smoothly!
Topic archived. No new replies allowed.