重新定义形式参数C++
Redefinition of formal parameters C++
让我试着改变我的方法。我有以下代码,我想将其转换为函数。但是,每当我尝试使用函数结构时,我的变量都会开始生成错误。感谢迄今为止为我提供支持的所有人。
#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();
}
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何将enable-if与模板参数和参数包一起使用