C++编译报错error C2086 'int func3'重定义问题求助
错误原因分析
error C2086是典型的标识符重定义报错,你的代码存在以下问题直接触发该报错:
- 全局作用域已经定义了名为
func3的函数,在入口函数(你此处笔误将main写成了manin,会导致程序找不到执行入口)中又先后两次尝试定义同名的int类型变量:- 第48行的
int func3;:第一次声明名为func3的int变量,和全局函数名冲突 - 第56行的
int func3(currentprice);:第二次尝试定义同名的int变量,直接触发重定义报错
- 第48行的
- 你对
func3的调用逻辑完全错误:定义的func3需要传入int指针、int索引两个参数,返回计算后的价格,你现有写法没有正确调用函数,也没有接收返回值 - 额外冗余问题:
func3定义的第二个参数index全程没有被使用,可以删除
修正后可运行代码参考
#include <iostream> #include <string> #include <array> #include <cmath> using namespace std; /* bool func1(int budget){ if( budget < 0 ) { cout << "Budget cannot be negative."<< endl; return false; } else { return true; } } bool func2(int num1, int num2, int num3){ // 修正原func2里重复定义num1/num2/num3的问题 if (num1 < 0 || num2 < 0 || num3 < 0 ) { cout << "All quantities must be positive."<< endl; return false; } else { return true; } } */ // 去掉冗余的index参数 int func3(int *p){ int currentprice = *(p) * 5 + *(p+1) * 10 + *(p+2) * 15 ; return currentprice; } // 修正入口函数拼写错误 int main() { int p1; int p2; int p3; int currentprice; cin >> p1 >> p2 >> p3 ; int position[3] ; position[0] = p1 ; position[1] = p2 ; position[2] = p3 ; // 正确调用func3函数接收返回值 currentprice = func3(position); cout << currentprice; return 0; }
内容的提问来源于stack exchange,提问作者sirac emin topçu
相关产品推荐
相关产品推荐

