如何解决 "未声明的标识符 "和 "形式参数的重新定义"?

问题描述 投票:0回答:1

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;
}
c++ pass-by-reference
1个回答
0
投票

你多次声明相同名称的变量。每个变量定义的名称应该是完全相同的。不允许使用相同的变量名,例如,作为函数参数和函数体中的变量名,例如。

void data(double &Vo) {
  double Vo = 0.0;   // Vo already exists with type double&
  // do something
}

请考虑以下提示。

  • 除非必要,不要使用全局变量,它们在大项目中很难调试。
  • 总是用一个值来初始化基本类型的变量(int, float, double, ...),否则就会得到一个 "随机 "的变量。

下面的代码在编译时应该没有错误,即使语义计算可能是错误的。我只是用了你的实现。

#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;
}
© www.soinside.com 2019 - 2024. All rights reserved.