Why is my call for a class function not recognised?

Hello,

I have made a program to convert between rectangular and polar coordinates using classes and friend functions. I am not sure why the compiler won't recognise the functions in one of the classes to display the data in an member object of that correspodning class.

Here is the error I receive
||=== Build: Debug in Project11.4 (compiler: GNU GCC Compiler) ===|
C:\Users\shish\Desktop\C++ programmes\Project11.4\main.cpp||In function 'int main()':|
C:\Users\shish\Desktop\C++ programmes\Project11.4\main.cpp|146|error: request for member 'display_coords' in 'A', which is of non-class type 'Rec_coord()'|

C:\Users\shish\Desktop\C++ programmes\Project11.4\main.cpp|147|error: request for member 'display_polar_coord' in 'B', which is of non-class type 'Pol_coord()'|

C:\Users\shish\Desktop\C++ programmes\Project11.4\main.cpp|149|error: request for member 'input_rect_coords' in 'A', which is of non-class type 'Rec_coord()'|

C:\Users\shish\Desktop\C++ programmes\Project11.4\main.cpp|150|error: request for member 'input_polar_coords' in 'B', which is of non-class type 'Pol_coord()'|

C:\Users\shish\Desktop\C++ programmes\Project11.4\main.cpp|152|error: request for member 'xval' in 'A', which is of non-class type 'Rec_coord()'|

C:\Users\shish\Desktop\C++ programmes\Project11.4\main.cpp|152|error: request for member 'yval' in 'A', which is of non-class type 'Rec_coord()'|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <cmath>

using namespace std;

class Pol_coord;

class Rec_coord
{
    friend void conv_pol(int, double, double, Pol_coord&, Rec_coord&);

    public:

        double xval;
        double yval;
        Rec_coord(double = 1.0, double = 1.0);
        void display_coords();
        void input_rect_coords();
};

Rec_coord::Rec_coord(double XVAL,double YVAL)
{
    xval = XVAL;
    yval = YVAL;
}

void Rec_coord::display_coords()
{
    cout << "The rectangular coordinates are " << endl;
    cout << "x = " << xval << endl;
    cout << "y = " << yval << endl;
}

void Rec_coord::input_rect_coords()
{
    cout << "Enter your rectangular coords" << endl;
    cin >> xval;
    cin >> yval;
}

class Pol_coord
{
    friend void conv_pol(int, double, double, Pol_coord&, Rec_coord&);

    public:
        double dist, theta;
        Pol_coord(double = 0, double = 0);
        void display_polar_coord();
        void input_polar_coords();
};

Pol_coord::Pol_coord(double DIST, double THETA)
{
    dist = DIST;
    theta = THETA;
}

void Pol_coord::display_polar_coord()
{
    cout << "The polar coordinates are" << endl;
    cout << "distance = " << dist << endl;
    cout << "theta = " << theta << endl;
}

void Pol_coord::input_polar_coords()
{
    cout << "Enter your polar coord" << endl;
    cout << "Enter your value for the radius " << endl;
    cin >> dist;

    cout << "Enter your value for theta" << endl;
    cin >> theta;

    cout << "Your chosen values for r and theta are" << endl;
    cout << "r = " << dist << endl;
    cout << "theta = " << theta << endl;
}

void conv_pol(int dir, double val1, double val2, Pol_coord &polref, Rec_coord &recref)
{
    if (dir == 1)
    {

        cout << "Converting from rectangular to polar coords" << endl;
        val1 = recref.xval;
        val2 = recref.yval;

        double r = 0.0;
        double theta = 0;

        r       = polref.dist = sqrt((pow(val1,2) + pow(val2,2)));
        theta   = polref.theta = tanh(val2 / val1);

        cout << "r =  " << r << endl;
        cout << "theta =  " << theta << endl;

    }else{

        cout << "Converting from polar to rectangular coords" << endl;

        val1 = polref.dist;
        val2 = polref.theta;

        double x = recref.xval = val1 * cos(val2);
        double y = recref.yval = val1 * sin(val2);

        cout << "x =  " << x << endl;
        cout << "y =  " << y << endl;
    }
}

int main()
{

    Rec_coord A();
    Pol_coord B();

    A.display_coords();
    B.display_polar_coord();

    A.input_rect_coords();
    B.input_polar_coords();

    conv_pol(2, A.xval, A.yval,B, A);

    return 0;
}
Change lines
1
2
    Rec_coord A();
    Pol_coord B();

to
1
2
    Rec_coord A;
    Pol_coord B;

(since, as they stand, they look like function prototypes).

Your angle conversion on line 92 is also badly wrong. The hyperbolic tangent function (tanh) actually has precious little to do with trigonometry. Look up atan2().
Last edited on
Hello Last chance.

Thanks very much! I can see that my object declarations were wrong and there was no need for the (). I've since changed over to atan like you said. When writing it I was offline and could not refer the website to look up tanh() and atan().

Here it is
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <cmath>

using namespace std;

class Pol_coord;

class Rec_coord
{
    friend void conv_pol(int, double, double, Pol_coord&, Rec_coord&);

    public:
        double xval;
        double yval;
        Rec_coord(double = 1.0, double = 1.0);
        void display_coords();
        void input_rect_coords();
};

Rec_coord::Rec_coord(double XVAL,double YVAL)
{
    xval = XVAL;
    yval = YVAL;
}

void Rec_coord::display_coords()
{
    cout << "The rectangular coordinates are " << endl;
    cout << "x = " << xval << endl;
    cout << "y = " << yval << endl;
}

void Rec_coord::input_rect_coords()
{
    cout << "Enter your rectangular coords" << endl;
    cin >> xval;
    cin >> yval;
}

class Pol_coord : public Rec_coord
{
    friend void conv_pol(int, double, double, Pol_coord&, Rec_coord&);

    public:
        double dist, theta;
        Pol_coord(double = 0, double = 0);
        void display_polar_coord();
        void input_polar_coords();
};

Pol_coord::Pol_coord(double DIST, double THETA)
{
    dist = DIST;
    theta = THETA;
}

void Pol_coord::display_polar_coord()
{
    cout << "The polar coordinates are" << endl;
    cout << "distance = " << dist << endl;
    cout << "theta = " << theta << endl;
}

void Pol_coord::input_polar_coords()
{
    cout << "Enter your polar coord" << endl;
    cout << "Enter your value for the radius " << endl;
    cin >> dist;

    cout << "Enter your value for theta" << endl;
    cin >> theta;

    cout << "Your chosen values for r and theta are" << endl;
    cout << "r = " << dist << endl;
    cout << "theta = " << theta << endl;
}

void conv_pol(int dir, double val1, double val2, Pol_coord &polref, Rec_coord &recref)
{
    cout << "Select 1 if you want to convert from rec to polar or 2 if you want to convert from polar to rect" << endl;
    cin >> dir;

    switch(dir){
        case 1:
        {

        cout << "Converting from rectangular to polar coords" << endl;
        val1 = recref.xval;
        val2 = recref.yval;

        double r = 0.0;
        double theta = 0;

        r       = polref.dist = sqrt((pow(val1,2) + pow(val2,2)));
        theta   = polref.theta = atan(
                                      val2 / val1);

        cout << "r =  " << r << endl;
        cout << "theta =  " << theta << endl;

        }

        case 2:
        {

        cout << "Converting from polar to rectangular coords" << endl;

        val1 = polref.dist;
        val2 = polref.theta;

        double x = recref.xval = val1 * cos(val2);
        double y = recref.yval = val1 * sin(val2);

        cout << "x =  " << x << endl;
        cout << "y =  " << y << endl;
        }
    }
}

int main()
{

    Rec_coord A;
    Pol_coord B;

    A.display_coords();
    B.display_polar_coord();
    A.input_rect_coords();
    B.input_polar_coords();

    conv_pol(2, A.xval, A.yval,B, A);

    return 0;
}
You would be safer with
atan2(val2,val1)
(note the TWO arguments) than your
atan(val2/val1)

There are two reasons.
(1) What happens due to division by zero if you use val2/val1 and val1 is zero?
(2) There are actually TWO angles in a complete circle with the same tangent. atan2 will correctly select the quadrant for you because its two arguments carry more information. atan will guess wrong 50% of the time. For example (-1.0)/(-1.0) is the same as (1.0)/(1.0) and atan(val2/val1) will give pi/4 for both, whereas it should be giving 5.pi/4 in the former case.

See
http://www.cplusplus.com/reference/cmath/atan2/
for atan2.
Last edited on
Topic archived. No new replies allowed.