C++ 错误:变量前应包含主表达式
刚开始编码时,我不知道在为派生类定义构造函数时必须为基类指定构造函数。我找到了一些答案并详细说明了它,但现在我无法理解其中的含义 现在的定义有问题吗 下面给出的代码仅供参考。该类人员和主要功能为固定代码,不能更改C++ 错误:变量前应包含主表达式,c++,constructor,compiler-errors,C++,Constructor,Compiler Errors,刚开始编码时,我不知道在为派生类定义构造函数时必须为基类指定构造函数。我找到了一些答案并详细说明了它,但现在我无法理解其中的含义 现在的定义有问题吗 下面给出的代码仅供参考。该类人员和主要功能为固定代码,不能更改 #include <iostream> #include <vector> using namespace std; class Person{ protected: string firstName; string lastName;
#include <iostream>
#include <vector>
using namespace std;
class Person{
protected:
string firstName;
string lastName;
int id;
public:
Person(string firstName, string lastName, int identification){
this->firstName = firstName;
this->lastName = lastName;
this->id = identification;
}
void printPerson(){
cout<< "Name: "<< lastName << ", "<< firstName <<"\nID: "<< id << "\n";
}
};
class Student : public Person{
private:
vector<int> testScores;
public:
Student(string firstName, string lastName, int id,vector<int>& scores):
Person(string firstName, string lastName, int id)
{
this->testScores=scores;
}
char calculate(){
vector <int>::iterator ptr;
int i=0;
int sum=0;
for(ptr=testScores.begin();ptr<testScores.end();ptr++)
{ i++;
sum+=*ptr;
}
int a = sum/i;
if(a>=90&&a<=100)
return 'O';
else if (a>=80&&a<90)
return 'E';
else if(a>=70&&a<80)
return 'A';
else if (a>=55&&a<70)
return 'P';
else if (a>=40&&a<55)
return 'D';
else
return 'T';
}
};
int main() {
string firstName;
string lastName;
int id;
int numScores;
cin >> firstName >> lastName >> id >> numScores;
vector<int> scores;
for(int i = 0; i < numScores; i++){
int tmpScore;
cin >> tmpScore;
scores.push_back(tmpScore);
}
Student* s = new Student(firstName, lastName, id, scores);
s->printPerson();
cout << "Grade: " << s->calculate() << "\n";
return 0;
#包括
#包括
使用名称空间std;
班主任{
受保护的:
字符串名;
字符串lastName;
int-id;
公众:
Person(字符串名、字符串名、整数标识){
这个->名字=名字;
此->lastName=lastName;
此->id=标识;
}
void printPerson(){
库特纽姆科里斯;
向量得分;
for(int i=0;i>tmpScore;
得分。推回(tmpScore);
}
学生*s=新学生(名字、姓氏、id、分数);
s->printPerson();
cout在您的Student
构造函数中,它希望调用基类构造函数,因此在将参数值传递给基类构造函数时需要省略数据类型。调用函数时不指定数据类型,仅在声明函数时指定
更改此项:
Student(stringfirstname、stringlastname、int-id、vector和scores):
Person(stringfirstname、stringlastname、int-id)
为此:
Student(stringfirstname、stringlastname、int-id、vector和scores):
人员(名、姓、id)
此外,您缺少#include
缺少#include。对图像进行向下投票,而不是文本。