C++调用方法

C++调用方法,c++,methods,switch-statement,C++,Methods,Switch Statement,如何使用数值参数创建OperationIt op方法,以便我可以通过operation1或operation3调用其他方法?我想我需要创建一个开关,但我不确定如何创建 int subtraction(int n1, int n2) { //e.g. of simple method return n1 - n2; } int multiplication (int n1, int n2){ return n1*n2; } int operation(int op) {

如何使用数值参数创建OperationIt op方法,以便我可以通过operation1或operation3调用其他方法?我想我需要创建一个开关,但我不确定如何创建

int subtraction(int n1, int n2) { //e.g. of simple method
    return n1 - n2;
}

int multiplication (int n1, int n2){
    return n1*n2;
}

int operation(int op) {
    // code that will call the method subtraction when I press 1.
    // same for multiplication...
}

int main () {
}

@编辑:添加默认案例,如下所示。另外,我认为你应该让变量名更具描述性。

你是在问如何使用if语句吗?为什么不参加讲座并学习这些东西呢?我想他是在问如何使用switch语句。需要默认值以防万一我想这就是我要找的。但它给了我一个错误,返回乘法int n1,int n2//不允许使用类型名称。function call中的参数太少默认值应该返回一个值我已经修复了这段代码中的两个调用。很明显,您不想尝试返回函数原型。错误1错误C2144:语法错误:“int”前面应加上错误2错误C2660:“Numbers::subgration”:函数对乘法不接受相同的0个参数,有什么问题吗?编辑:谢谢,现在可以工作了。
int operation(int op, int n1, int n2) {
    switch( op )
    {
        case 1:
            return subtraction(n1, n2);

        case 2:
            return multiplication(n1, n2);

        default:
            // default case, when the op value is neither 1 or 2
            cout << "Error! << endl;
            return 0;
    }
}