Why Can't I make this into a class?

Why can't I make this into a class? (It works fine as functions alone but, once I put it into a class it generates errors, also I know I havent made any Private data yet, I will once I can get it to compile without errors.)

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef CALCULATORFUNCTIONS_H_INCLUDED
#define CALCULATORFUNCTIONS_H_INCLUDED

class calculator{
void functionsquare(){
int total= 0;
int choice = 0;

cout << "Enter a number to square: ";
cin >> choice;
total = choice * choice;
cout<<"\n"<<choice<<" squared is: "<<total <<"\n\n\n" <<endl;

}


void functionadd(){
const int size = 50;

long int iarray[size];

for (int i=0; i<size; i++){
iarray[i]=0;
}


long int number;

long int total = 0;

for(int i=0; i<size; i++){

cout<< "Enter a number to add, Enter -1 to get total.\n";
cin >> number;

if(number!=-1){

iarray[i]= number;
}
else
break;
}

for(int i=0; i<size ; i++){

total += iarray[i];
}


cout <<"\nThe total is: " <<total<<"\n\n\n"<<endl;


}

void functionsubtract(){
const int size = 50;

long int iarray[size];

for (int i=0; i<size; i++){
iarray[i]=0;
}


long int number;

long int total = 0;

for(int i=0; i<size; i++){

cout<< "Enter a number to subtract, Enter -1 to get total.\n";
cin >> number;

if(number!=-1){

iarray[i]= number;
}
else
break;
}

for(int i=0; i<size ; i++){

total -= iarray[i];
}


cout <<"\nThe total is: " <<total<<"\n\n\n"<<endl;


}

void functiondivide(){

long int num1, num2, total;

int remainder;

cout<< "Enter a number to divide.\n";

cin >> num1;

cout<<"\nEnter another number to divide.\n";

cin>>num2;

if((num1 == 0) || (num2 == 0)){
cout <<"\n\nYou cant divide by zero! \n\n";
functiondivide();
}
else

total = num1 /num2;

remainder = num1 % num2;

cout <<"\nThe total is: " <<total<< ", The remainder is: "<<remainder<<"\n\n\n"<<endl;

}



void displaymessage(){

int getfunc = 0;


while(getfunc != -1){
cout<<"Enter \"1\" to add.\nEnter \"2\" to square.\nEnter \"3\" to subtract.\nEnter \"4\" to divide.\n\nEnter \"-1\" to quit. "<<endl;
cin>>getfunc;
cout<<endl;

if(getfunc == 1)
functionadd();
else if (getfunc==2)
functionsquare();
else if (getfunc==3)
functionsubtract();
else if(getfunc==4)
functiondivide();
else if( getfunc == -1)
break;

else{
cout<<"\n\nYou entered an invalid answer.\n\n\n\n";
displaymessage();
}

}
}
}

#endif // CALCULATORFUNCTIONS_H_INCLUDED



These functions work fine without class, but when I put them into the class I get. I get these errors.



Compiling: C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\main.cpp

C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\main.cpp:14: error: new types may not be defined in a return type
C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\main.cpp:14: error: extraneous `int' ignored
C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\main.cpp:14: error: `main' must return `int'
C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\/CalculatorFunctions.h: In function `int main(...)':
C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\/CalculatorFunctions.h:123: error: `void calculator::displaymessage()' is private
C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\main.cpp:18: error: within this context
Process terminated with status 1 (0 minutes, 1 seconds)
5 errors, 0 warnings



And this is what main looks like..



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <cstdlib>
#include <iostream>
using namespace std;

#include "CalculatorFunctions.h"


int main()
{

calculator cal;

cal.displaymessage();

cin.get();
cin.get();
system("PAUSE");
return EXIT_SUCCESS;
}
Last edited on
On line 4, change "class" to "struct".
Now I get 3 errors instead of 5, thanks but I still need to figure out what these other errors mean.

1
2
3
4
5
6
Compiling: C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\main.cpp
C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\main.cpp:14: error: new types may not be defined in a return type
C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\main.cpp:14: error: extraneous `int' ignored
C:\Documents and Settings\HP_Administrator\Desktop\MinnesotaNMC\CodeBlocksC++\Calculator Projects\Calculator001\main.cpp:14: error: `main' must return `int'
Process terminated with status 1 (0 minutes, 0 seconds)
3 errors, 0 warnings 
Yeah, what is said below.
Last edited on
You need a ';' at the end of your class definition. Btw, this looks like a really bad use of a class...
Thank you, wow I had read that somewhere before. It works now. Thanks L B and firedraco.
Also, just a note for you based on what firedraco said about your use of a class, you may want to use a namespace instead.
Topic archived. No new replies allowed.