当我编译代码时,我在第47行:
上收到错误 [Error] invalid use of member (did you forget the '&' ?)
我不确定为什么,因为我正在尝试为total_score
添加分数。
第二个问题是错误说目标' main.o'失败。这不在我的代码中,但会显示一个标记为makefile.win的新选项卡
这是我的代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
class Student
{
public:
void student_name();
void quiz_score();
void total_score();
void average_score();
private:
int the_number_of_quizs;
int sum_of_scores;
double score_average;
};
void Student::student_name()
{
string name;
cout << "Please enter your name" << endl;
cin >> name;
cout << endl;
}
void Student::quiz_score()
{
cout << "What was your score on the quiz?: " << endl;
int score;
cin >> score;
total_score += score;
the_number_of_quizs++;
}
void Student:: average_score()
{
score_average= sum_of_scores/ the_number_of_quizs;
}
void Student:: total_score()
{
cout << "Total score: " << sum_of_scores << endl;
}
int main ()
{
Student student1;
student1.quiz_score();
student1.student_name();
student1.total_score();
student1.average_score();
return 0;
}
答案 0 :(得分:2)
你有void total_score()
total_score 是一个返回void的函数,这就是以下内容无效的原因:
total_score += score;
我怀疑你打算使用:
sum_of_scores += score;
答案 1 :(得分:0)
如果我完全理解你的所作所为,那么你的主要问题似乎来自你所拥有的那条线:
total_score += score;
运行它会标记你:
main.cpp:38:9: error: invalid use of member function ‘void Student::total_score()’ (did you forget the ‘()’ ?)
total_score += score;
^~~~~~~~~~~
您似乎想将学生的前一个分数添加到他/她的当前分数中。如果我的假设是正确的,那么我认为total_score
不应该是你需要的,你需要sum_of_scores
这应该以下面的代码结束你的新代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
class Student
{
public:
void student_name();
void quiz_score();
void total_score();
void average_score();
private:
int the_number_of_quizs;
int sum_of_scores;
double score_average;
};
void Student::student_name()
{
string name;
cout << "Please enter your name" << endl;
cin >> name;
cout << endl;
}
void Student::quiz_score()
{
cout << "What was your score on the quiz?: " << endl;
int score;
cin >> score;
sum_of_scores += score; //This is where you're correction is.
the_number_of_quizs++;
}
void Student:: average_score()
{
score_average= sum_of_scores/ the_number_of_quizs;
}
void Student:: total_score()
{
cout << "Total score: " << sum_of_scores << endl;
}
int main ()
{
Student student1;
student1.quiz_score();
student1.student_name();
student1.total_score();
student1.average_score();
return 0;
}