Unsure of why program is getting errors

So in my code I've setup height and radius to be private, and when I try to run the code the error I get is 'int Cone::height' is private(line 17) in this context(line 46) What am I doing wrong? This is my code
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
//Brian Doan CIS 22B
//Summer 2015
//Assignment B
//Problem B1
//Write one program containing all 6 of the listed string exercises.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const double PI = 3.14159265358979323846;


class Cone
{
private:
    int height;
    int radius;

public:
    void setUp(int, int);
    int getVolume();
    void output();
};


void input(Cone *);
void setUp(Cone *, int, int);
int getVolume(Cone *);
void output(Cone *);

int main()
{
    struct Cone *ptr = new Cone;
    double height;
    double radius;
    input(ptr);
    setUp(ptr, 6, 2);
    output(ptr);
    return 0;
}
void input(Cone * ptr)
{
    int height, radius;
    cout << "Enter height of the cone followed by the radius." << endl;
    cin >> ptr->height;
    cin >> ptr->radius;
    cout << endl;
}

void Cone::setUp(int paramHeight, int paramRadius)
{
    cout << "Now running function setUp" << endl << endl;
    height = paramHeight;
    radius = paramRadius;
}


int Cone::getVolume()
{
    int Volume;
    Volume = ( PI * radius * radius * height )  / 3;
    return Volume;
}

void Cone::output()
{
    cout << "Here is the volume of a cone with radius " << radius << " and height " << height
        << " \nrounded down to the nearest whole number" << endl;
    cout << "Volume: " << getVolume() << endl;
}

/* Execution Results
*/

Thank you!
It is what it says:

member height is private. That means only member functions of class can access it.
Function input is not a member, so it cannot access it.
So I should include input into the private right?
Use a setUp member function which you created for exactly this situation.
I'm not sure how I should be going about this, how do I use setUp to make input be able to access private?
1
2
3
4
5
6
7
8
9
void input(Cone * ptr)
{
    int height, radius; //You declared these variables for a reasin. USE them.
    cout << "Enter height of the cone followed by the radius." << endl;
    cin >> /*ptr->*/height;
    cin >> /*ptr->*/radius;
    cout << endl;
    //Pass those two values to setUp function through ptr pointer
}
Last edited on
So I've updated the code a bit and tried to pass height and radius to setUp but it's giving me the error "48:9: error: request for member 'setUp' in 'ptr', which is of pointer type 'Cone*' (maybe you meant to use '->' ?)
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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const double PI = 3.14159265358979323846;


class Cone
{
private:
    int height;
    int radius;

public:
    void setUp(int, int);
    int getVolume();
    void output();
};


void input(Cone *);
void setUp(Cone *, int, int);
int getVolume(Cone *);
void output(Cone *);
/* ******************main***********************
 has 3 variables, new to obtain space, calls input, setUp and output.
*/
int main()
{
    struct Cone *ptr = new Cone;
    double height;
    double radius;
    input(ptr);
    setUp(ptr, 6, 2);
    output(ptr);
    return 0;
}
/* ******************input***********************
 takes height and radius as base of reference,  reads in height and radius from user.
*/
void input(Cone * ptr)
{
    int height, radius;
    cout << "Enter height of the cone followed by the radius." << endl;
    cin >> height;
    cin >> radius;
    ptr.setUp(height, radius);
    cout << endl;
}
/* ******************setUp***********************
 takes 2 parameters by value, height and radius .
*/
void Cone::setUp(int paramHeight, int paramRadius)
{
    cout << "Now running function setUp" << endl << endl;
    height = paramHeight;
    radius = paramRadius;
}
/* ******************getVolume***********************
 computers and returns volume.
*/

int Cone::getVolume()
{
    int Volume;
    Volume = ( PI * radius * radius * height )  / 3;
    return Volume;
}
/* ******************output***********************
 calls getvolume to get the volume. prints height and radius..
*/
void Cone::output()
{
    cout << "Here is the volume of a cone with radius " << radius << " and height " << height
        << " \nrounded down to the nearest whole number" << endl;
    cout << "Volume: " << getVolume() << endl;
}

/* Execution Results
*/

What should I do?
Well. error message pointed to a problem (trying to use meber selection operator on pointer) and solution (use pointer member selection)
Thanks for all your help, I ended up fixing it somehow.
Topic archived. No new replies allowed.