重新定义形式参数C++

Redefinition of formal parameters C++

本文关键字:参数 C++ 定义 新定义      更新时间:2023-10-16

让我试着改变我的方法。我有以下代码,我想将其转换为函数。但是,每当我尝试使用函数结构时,我的变量都会开始生成错误。感谢迄今为止为我提供支持的所有人。

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;
void names(string names[9]);
void grades(double scores[10][5]);
void getData(double scores[10][5]);

void getData(int scores[10][5])
{
}
void names(string names[9])
{

}
void grades(double scores[10][5])
{
}
int main()
{
//create a string of names to run parallel to thestring containg the students average
string names[9] = { "Johnson", "Aniston", "Cooper", "Blair",
    "Clark", "Kennedy", "Bronson", "Sunny", "Smith" };
int scores[10][5] = { { 85, 83, 77, 91, 76 },
{ 80, 90, 95, 93, 48 },
{ 78, 81, 11, 90, 73 },
{ 92, 83, 30, 69, 87 },
{ 23, 45, 96, 38, 59 },
{ 60, 85, 45, 39, 67 },
{ 77, 31, 52, 74, 83 },
{ 93, 94, 89, 77, 97 },
{ 79, 85, 28, 93, 82 },
{ 85, 72, 49, 75, 63 } };

// Calculate the grades per student and store to the "grades" string
double sum = 0;
double avg = 0;
double grades[10];
//start at the first student.Then move to the next after the average has been computed. 
for (int row = 0; row < 11; row++)
{
    for (int col = 0; col < 5; col++)
    {
        //calculate the sum so it can bedevided by 5
        sum += scores[row][col];
    }
    avg = sum / 5;
    //load the students average into an array
    grades[row] = avg;
    //reset the average so that it does not compound
    sum = 0;
    avg = 0;
}
//print names
for (int i = 0; i < 9; i++)
{
    cout << names[i] << endl;
}

//print grades
for (int i = 0; i < 10; i++)
{
    cout << grades[i] << endl;
}
system("pause");
}

在这些函数中

void getData(int scores[10][5])
{
int scores[10][5] = { { 85, 83, 77, 91, 76 },
//...

 void names(string names[9])
{
string names[9] = { "Johnson", "Aniston", "Cooper", "Blair",
    "Clark", "Kennedy", "Bronson", "Sunny", "Smith" };
//...

您在函数主体的最外层范围内声明局部变量的方式与其参数名称相同。(例如,在第一个函数中,参数名称是 score,局部变量也被定义为具有相同名称的 scores)C++标准认为此类代码格式不正确。

也不清楚为什么要重新定义这些函数参数。

同样,起初您将函数名称声明为具有 int 数组作为其参数

void names(int names[9]);

下面您将其定义为将字符串数组作为其参数。

void names(string names[9])

此外,在主要方面既没有定义名称也没有定义等级

你的代码没有意义。

至少我认为您必须在函数 main 中定义一个具有此名称的数组,而不是函数名称。

也许这样的事情会更好。它首先创建一个名为学生class(不要与一班学生混淆!然后继续定义可用于访问类中的私有值的函数和三个构造函数。

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class student
{
    private:
    std::string name_;
    int scores_[5]; // A vector here might be more useful, if you might add more scores later.
    // eg.
    // std::vector<int> scores_;
    double grade_;
    void storeGrade()
    {
         int sum = 0;
         for (int i=0; i<5; i++) // if using a vector, you could write this with iterators,
         // using scores_.begin(), scores_.end etc
         {
              sum+=scores_[i];
         }
         grade_=(sum/5); // using a counter within the loop would be better if you might have different numbers of scores
     }
     public:
     std::string getName()
     {
          return name_;
     }
     double getGrade()
     {
          return grade_;
     }
     void setScore(int score, int position)
     {
          // with vectors, I think you can use score_.push_back(score) here
          // using instead an array of ints, you need the second parameter, position
          score_[position] = score;
     }
     // Constructors, taking three different sets of parameters.
    student(std::string name)
    {
        name_=name;
    }
    student(std::string name, int score)
    {
        name_=name;
        score_[0]=score;
    }
    student(std::string name, int score[5])
    {
        name_=name;
        for (int i=0; i<5; i++) // Not sure if this is strictly necessary, but should work fine
        {
            score_[i]=score[i];
        }
    }
}
int main()
{
     student John(John, {95, 17, 20, 56, 28}) // not sure if this will pass as a score[], but if not, 
     //you can pass a real array, or use John(John); and then use John.setScore(...);
     John.storeGrade();
     double JohnsGrade = John.getGrade();
}