Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: two or more data types in declaration of function?

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;
}   
like image 425
Bucky763 Avatar asked May 25 '26 00:05

Bucky763


1 Answers

ans needs a type (probably string), add needs quotes ("add"), CalcClass.calcObject; needs to be CalcClass 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 than CalcClass.add();).

Also, as Mahesh says, pickFunction(); needs to be in main. This should look as follows:

void pickFunction(){
    //code
}
int main() {
    pickFunction();
    //...
}
like image 82
Michael Dorst Avatar answered May 26 '26 12:05

Michael Dorst



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!