Problems with class name

Jun 3, 2018 at 8:44pm
I'm very new to classes and don't understand the problem I'm having here. I can write other code with classes and they run fine. I'm getting 4 errors; Circle not declared in this scope, circOne not declared in this scope, Square not declared in this scope, sqOne not declared in this scope. What am I missing here?

Thank you

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
 main
#include <iostream>
#include <string>
#include "Circle.h"
#include "Square.h"
using namespace std;

int main()
{
Circle circOne;
    cout<<"Enter radius of circle\n";
    cin>>circOne.getRadius;
    cout<< "Area of circle with radius "
              <<circOne.theirRadius << " is " <<circOne.area;
Square sqOne;
    cout<<"Enter the width and height of the square\n";
     cin>>sqOne.getWidth>>sqOne.getHgt;
    cout<<"The area of the square is"<<sqOne.area;
    return 0;
}
Circle.h
#include <iostream>
using namespace std;

#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
private:
    float theirRadius;
    float area;
public:
    float getRadius();
    float figureArea();
};
#endif // CIRCLE_H
Square.h
#include <iostream>
using namespace std;
#ifndef SQUARE_H
#define SQUARE_H
class Square
{
    public:
        float getWidth();
        float getHgt();
        float figSqArea();
    private:
        float theirWidth;
        float theirHgt;
        float area;
};
#endif // SQUARE_H
Circle.cpp
#include "Circle.h"
float Circle::getRadius()
{
    return theirRadius;
}
float Circle::figureArea()
{
area = 3.14 * theirRadius * theirRadius;
return area;
}
Square.cpp
#include "Square.h"
float Square::figSqArea()
{
  area = theirWidth * theirHgt;
  return area;
}
float Square::getWidth()
{
    return theirWidth;
}
float Square::getHgt()
{
    return theirHgt;
}

Jun 3, 2018 at 8:59pm
closed account (E0p9LyTq)
You can't access private class members outside the class scope.

You are using class functions as variables and class variables as functions.

Jun 3, 2018 at 9:32pm
Thank you for replying!

I'm not sure I understand your advice. It's like main isn't seeing the class name and label.
I call to the functions to get the variables. The variables are private and the functions are public. I've tried everything I can find.This is my first foray with classes.
I'm baffled. It's probably something really simple that I don't get!

Thanks again!
Jun 3, 2018 at 10:10pm
circOne.getRadius
That's not how to call a function.


circOne.getRadius() is how to call a function.


So your code should be cin>>circOne.getRadius();. But this is still wrong.

circOne.getRadius() is a function call that returns a float. You might as well write cin>>7;


You're trying to set a value inside a class.

1
2
3
4
void Circle::setRadius(float new_radius_value)  //NEW FUNCTION
{
    theirRadius = new_radius_value;
}


called as

1
2
3
float input;
cin >> input;
circOne.setRadius(input);
Last edited on Jun 3, 2018 at 10:12pm
Jun 3, 2018 at 11:08pm
I could call the functions just fine that way before I created the classes. The entire program worked fine before I created the classes. All function calls worked great just as they are written. There is some problem with how I named the classes, or declared them. Or possibly the three different files. I'm baffled!

Thank you for the response
Jun 4, 2018 at 7:51am
I could call the functions just fine that way before I created the classes.
No you couldn't.

Maybe you're mixing up functions and variables.

1
2
3
4
5
6
7
8
9
struct A
{
  int B;
  int C;
}

A someObject;

someObject.c; // This is NOT a function call 



Errors come with line numbers. Tell us the line number and the file and the error.
Last edited on Jun 4, 2018 at 7:57am
Topic archived. No new replies allowed.