Need some help with class scopes?

Apr 6, 2012 at 1:26am
When I run it, it says that 'in member function 'void calc::over() error main() was not declared in this scope'. I don't know how to make it recognize 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
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
#include <iostream>
using namespace std;

class calc
{

public:
    void add()
{
	double a,b,c;
	cout<<"Put in the first number: ";
	cin>>a;
	cout<<"\n";
	cout<<"Put in the second number: ";
	cin>>b;
	cout<<"\n";
	c = a + b;
	cout<<c;
}

void sub()
{
	double a,b,c;
	cout<<"Put in the first number: ";
	cin>>a;
	cout<<"\n";
	cout<<"Put in the second number: ";
	cin>>b;
	cout<<"\n";
	c = a - b;
	cout<<c;
}

void mult()
{
	double a,b,c;
	cout<<"Put in the first number: ";
	cin>>a;
	cout<<"\n";
	cout<<"Put in the second number: ";
	cin>>b;
	cout<<"\n";
	c = a * b;
	cout<<c;
}

void div()
{
	double a,b,c;
	cout<<"Put in the first number: ";
	cin>>a;
	cout<<"\n";
	cout<<"Put in the second number: ";
	cin>>b;
	cout<<"\n";
	c = a / b;
	cout<<c;
}

void sq()
{
	double a,b;
	cout<<"Put in the number to be squared: ";
	cin>>a;
	cout<<"\n";
	b = a*a;
	cout<<b;
}

void over()
{
	char a;
	cout<<"My good sir I believe you have gone over the limit."<<endl;
	cout<<"Would you like a do-over?"<<endl;
	cout<<"y or n?"<<endl;
	cin>>a;
	if (a=='y') {main();cout<<"\n";}
	else if (a=='n') {cout<<"Then my good sir, get out.";}
}


};


int main()
{
	int choice;
	char a;
	calc calcob;
	cout<<"Do you want to do addition, subtraction, etc.?"<<endl;
	cout<<"1 - Addition"<<endl;
	cout<<"2 - Subtraction"<<endl;
	cout<<"3 - Multiplication"<<endl;
	cout<<"4 - Division"<<endl;
	cout<<"5 - Square a Number"<<endl;
	cin>>choice;
	cout<<"\n";
	if (choice==1) calcob.add();
	else if (choice==2) calcob.sub();
	else if (choice==3) calcob.mult();
	else if (choice==4) calcob.div();
	else if (choice==5) calcob.sq();
	else calcob.over();
	cout<<endl;
	cout<<endl;
	cout<<endl;
	cout<<"Do you want to calculate more, y or n?"<<endl;
	cin>>a;
	if (a=='y')
	{
	cout<<endl;
	cout<<endl;
	cout<<endl;
	main();
	}
	cin.get();
	cin.get();
	return 0;
}





Apr 6, 2012 at 2:23am
if (a=='y') {main();cout<<"\n";}

In line 77, lose the main() and you'll be good to go.
Apr 6, 2012 at 2:37am
Hmm. I was wanting to shave it so if the user entered an invalid number (aka below 1 and above 5) it would ask them for a do-over, completely restarting a program.
Apr 6, 2012 at 7:12am
why use a class? struct would be better with only variables whose scopes are in single functions.
Apr 6, 2012 at 10:12am
Why would struct be better when class is the same thing?

Using namespace is more appropriate because it doesn't make much sense having a calc object without state. To use a namespace instead just change class calc to namespace calc, remove calc calcob; from line 89, change calcob.add(); to calc::add(); and do the same with the other function calls.
Apr 6, 2012 at 10:16am
Also calling main() is not allowed so don't that. Maybe you could have a function returning value that you use as a loop condition for a loop in main. The loop just have to contain all the code that should be repeated.
Topic archived. No new replies allowed.