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.)
#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(){
constint size = 50;
longint iarray[size];
for (int i=0; i<size; i++){
iarray[i]=0;
}
longint number;
longint 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;
}
elsebreak;
}
for(int i=0; i<size ; i++){
total += iarray[i];
}
cout <<"\nThe total is: " <<total<<"\n\n\n"<<endl;
}
void functionsubtract(){
constint size = 50;
longint iarray[size];
for (int i=0; i<size; i++){
iarray[i]=0;
}
longint number;
longint 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;
}
elsebreak;
}
for(int i=0; i<size ; i++){
total -= iarray[i];
}
cout <<"\nThe total is: " <<total<<"\n\n\n"<<endl;
}
void functiondivide(){
longint 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();
elseif (getfunc==2)
functionsquare();
elseif (getfunc==3)
functionsubtract();
elseif(getfunc==4)
functiondivide();
elseif( 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
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