C++编译报错求助:‘arbitrage_two_diff_odds’未在此作用域声明
解决C++套利投注程序的编译错误及代码优化建议
你遇到的‘arbitrage_two_diff_odds’ was not declared in this scope编译错误,核心原因是C++编译器要求函数在被调用前必须先被声明或定义。你的what_do_you_want_to_do函数里提前调用了arbitrage_two_diff_odds,但这个函数的定义在后面,编译器没法识别它。下面是具体的修复方案和代码优化建议:
1. 添加函数前置声明
在what_do_you_want_to_do函数定义之前,添加arbitrage_two_diff_odds的函数签名,让编译器提前知道这个函数的存在:
// 前置声明套利计算函数 double arbitrage_two_diff_odds(double a, double b, double c);
2. 修正main函数的调用错误
你的what_do_you_want_to_do函数设计上不需要传入参数,而且它返回double类型,不能直接赋值给int变量。修改main函数和what_do_you_want_to_do的函数签名:
// 调整函数签名,不需要传入参数 double what_do_you_want_to_do(){ // ... 原有代码 } int main() { what_do_you_want_to_do(); // 直接调用即可 return 0; }
3. 修复函数逻辑冗余
arbitrage_two_diff_odds函数里的else分支后重复输出了同一句提示,删掉重复的行:
// 原代码里的重复输出,保留一句即可 else std::cout << "No profit for the odds entered, try different one again"<< std::endl; // 删掉下面这句重复的 // std::cout << "No profit for the odds entered, try different one again"<< std::endl;
4. 补充函数返回值
C++要求非void类型的函数必须返回值,给arbitrage_two_diff_odds和what_do_you_want_to_do添加合适的返回值(比如返回计算后的利润,或者0表示执行完成)。
修改后的完整代码
/*Programm for making decision on arbitrage betting*/ #include <iostream> // 前置声明套利计算函数 double arbitrage_two_diff_odds(double a, double b, double c); // Prompting user to select an option. double what_do_you_want_to_do(){ double a, b, c; int z; // 把z的声明移到函数内部,不需要外部传入 std::cout << "What do you want to do" << std::endl; std::cout << "For calculating Percentage & Profit for a single bet, PRES 1" << std::endl; std::cout << "For calculating Arbitrage Percentantage & Profit for two odds, PRESS 2" << std::endl; std::cout << "For calculating Arbitrage Percentantage & Profit for three odds, PRESS 3" << std::endl; std::cin >> z; //Using switch to branch. switch(z) { case 1: //call function 1,后续可以补充实现 break; case 2: //call function 2 arbitrage_two_diff_odds(a, b, c); break; case 3: //call function 3,后续可以补充实现 break; default: std::cout << "Invalid option selected!" << std::endl; break; } return 0.0; // 返回一个默认值 } //function for calculation of the arbitrage for two different odds. double arbitrage_two_diff_odds(double a, double b, double c){ //Prompting user for imput. std::cout << "Enter your values in this format \"1st odd, 2nd odd, investment\": "; std::cin >> a >> b >> c; //Calculating the sum of the percentage of the two different odds. double diff_two_odds = ((1/a)*100) + ((1/b)*100); //maniplation if (diff_two_odds <= 90) { double calc_profit = c/diff_two_odds; double idividual_bet1 = (c*((1/a)*100))/diff_two_odds; double idividual_bet2 = (c*((1/b)*100))/diff_two_odds; std::cout << "The arbitrage odds you put in are "<<a<< " and "<<b<<"."<< std::endl; std::cout << "The percentage gain for individual odd are "<<((1/a)*100)<<"% and "<<((1/b)*100)<<"% respectively."<<std::endl; std::cout << "The individual bet for your propose investment are "<<idividual_bet1<<"$ for "<<a<< " and "<<idividual_bet2<<"$ for " <<b<< " respectively, which equals "<<c<<"."<<std::endl; std::cout << "From your proposed investment of "<<c<<"$, Your arbitrage profit is "<<calc_profit<< std::endl; return calc_profit; // 返回计算出的利润 } else { std::cout << "No profit for the odds entered, try different one again"<< std::endl; return 0.0; // 无利润时返回0 } } int main() { what_do_you_want_to_do(); return 0; }
额外提示
- 后续可以补充实现case 1和case 3对应的函数,同样要注意前置声明
- 可以添加输入验证,比如确保用户输入的赔率和投资金额是正数,避免除以0的错误
内容的提问来源于stack exchange,提问作者Paschal
相关产品推荐
相关产品推荐

