I'm working on a program to calculate average acceleration and I use 3 function ( by pass reference method) after writing my code this error happens and "error C2082: redefinition of formal parameter 'Vo'".I've google it and I barely understand it.Can anyone explain to me why this happens and how to solve this?thank you for helping!
/* lab assessment 4 kiraan pecutan*/
#include <iostream>
using namespace std;
void data(double& Vo,double& Vt,double& t);
void calculate(double& sum);
void output(double& out);
double Vo,Vt,t,sum,out;
int main()
{
cout<<"please enter your velocity(Vo=m/s)\n,velocity(Vt=m/s)\nand time(s=second)\n\n";
data(Vo,Vt,t);
calculate(sum);
output( out);
return 0;
}
void data(double& Vo,double& Vt,double& t)
{
double Vo,Vt,t;
cin>>Vo;
cin>>Vt;
cin>>t;
cout<<"your Vo="<<Vo<<" ,Vt="<<Vt<<" and T="<<t<<"\n\n";
}
void calculate(double& sum )
{
double Vt,Vo,t;
sum=(Vt-Vo)/t;
}
void output(double& out)
{
double sum;
cout<<"the acceleration ="<<sum;
}
你多次声明相同名称的变量。每个变量定义的名称应该是完全相同的。不允许使用相同的变量名,例如,作为函数参数和函数体中的变量名,例如。
void data(double &Vo) {
double Vo = 0.0; // Vo already exists with type double&
// do something
}
请考虑以下提示。
下面的代码在编译时应该没有错误,即使语义计算可能是错误的。我只是用了你的实现。
#include <iostream>
using namesace std;
//--------------------------------------------------------------------------//
void data(double &Vo, double &Vt, double &t);
void calculate(double &Vo, double &Vt, double &t, double &sum);
void output(double &out);
//--------------------------------------------------------------------------//
int main() {
double Vo = 0.0;
double Vt = 0.0;
double t = 0.0;
double sum = 0.0;
double out = 0.0;
cout << "please enter your velocity(Vo=m/s)\n,velocity(Vt=m/s)\nand time(s=second)\n\n";
data(Vo, Vt, t);
calculate(Vo, Vt, t, sum);
output(out);
return 0;
}
//--------------------------------------------------------------------------//
void data(double &Vo, double &Vt, double &t) {
cin >> Vo;
cin >> Vt;
cin >> t;
cout << "your Vo=" << Vo << " ,Vt=" << Vt << " and T=" << t << "\n\n";
}
//--------------------------------------------------------------------------//
void calculate(double &Vo, double &Vt, double &t, double &sum) {
sum = (Vt - Vo) / t;
}
//--------------------------------------------------------------------------//
void output(double &out) {
cout << "the acceleration =" << out;
}