My plan was to make a simple addition calculator, and move on from there.
Remember, this is my first day coding.
#include <iostream>
#include <string>
using namespace std;
int a;
int b;
int sum;
string ans;
class CalcClass{
public:
int add (int a, int b) {
cout << "Pick the numbers you want to add" << endl;
cin >> a >> b;
sum = a + b;
return sum;
}
};
Added string ans; (at the top). Now I’m getting an "error: no matching function for call to 'CalcClass::add()'"
Why would it be saying this if I already created calcObject and used calcObject.add(); to call the function?
void pickFunction(){
cout << "What Function do you want to do? \n Add, Subtract, multiply, or divide? ";
cin >> ans;
if (ans == "add"){
CalcClass calcObject;
calcObject.add();
}
int main(){
pickFunction();
cout << "Your answer is : " << sum << endl;
return 0;
}
ansneeds a type (probablystring),addneeds quotes ("add"),CalcClass.calcObject;needs to beCalcClass calcObject;.‘dot’ syntax (
x.y) is used for accessing data or functions that are stored inside of an object, not a class (e.g.calcObject.add();rather thanCalcClass.add();).Also, as Mahesh says,
pickFunction();needs to be inmain. This should look as follows: