gpt4 book ai didi

c++ - ‘]’ 标记之前的预期主表达式

转载 作者:行者123 更新时间:2023-11-30 00:54:01 25 4
gpt4 key购买 nike

<分区>

我尝试用C++(Kdevelop platfrom)中的类写了一段简单的计算ave的代码。我的头文件是:

#include <string>
class gradeBank
{
public:
gradeBank(std::string, float [], int n);
void setCourseName(std::string Coursename);
std::string getCourseName();
void gradeProcess();
float Max();
float Min();
double average();
//
private:
std::string Coursename;
int n; // n= number of students, size of grade-array
float grade[];
};

我的头文件相关的cpp是:

#include <iostream>
#include <stdio.h>
#include "gradeBank.h"
//
gradeBank::gradeBank(std::string name, float gradeArray[], int n)
{
setCourseName(name);
float grade[n];
for (int i = 0; i < n; i++)
grade[i] = gradeArray[i];
}
//
void gradeBank::setCourseName(std::string name)
{
Coursename = name;
}
//
std::string gradeBank::getCourseName()
{
return Coursename;
}
//
void gradeBank::gradeProcess()
{
std::cout << "class average is: " << average() << std::endl;
std::cout << "Maximum grade is: " << Max() << std::endl;
std::cout << "minimum grade is: " << Min() << std::endl;
}
//
float gradeBank::Max()
{
float max = 0.0;
for (int i = 0; i < n; i++)
max = max > grade[i] ? max : grade[i];
return max;
}
//
float gradeBank::Min()
{
float min = 0.0;
for (int i = 0; i < n; i++)
min = min > grade[i] ? min : grade[i];
return min;
}
//
double gradeBank::average()
{
double sum = 0.0, ave;
for (int i = 0; i < n; i++)
sum += grade[i];
ave = sum / n;
return ave;
}

程序的主体是:

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include "gradeBank.h"
#include <iostream>
//
int main()
{
std::string name;
int num, i;
float gradeArray[num];
std::cout << "Please enter the name of the course: ";
std::cin >> name;
std::cout << "Please enter number of students: ";
std::cin >> num;
for (i = 0; i < num; i++)
{ std::cout << "Studnet " << i << " : ";
std::cin >> gradeArray[i];
std::cout << std::endl;
}
gradeBank t1(name, gradeArray[], num);
t1.gradeProcess();
std::cin.get();
return 0;
}

我收到以下错误:

 In function ‘int main()’:
...e/main.cpp:23: error: expected primary-expression before ‘]’ token

谢谢大家的帮助

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com