当我编译代码时,我在第 47 行 遇到错误:
[错误] 成员使用无效(您是否忘记了“&”?)
我不确定为什么,因为我正在尝试将分数添加到 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;
}
最佳答案
你有这个 void total_score()
total_score 是一个返回 void 的函数,这就是以下无效的原因:
total_score += score;
我怀疑你打算使用:
sum_of_scores += score;
关于c++ - 我收到 2 个我无法解决的错误; 1 无效使用成员(你忘记了 '&' 吗?)和目标 'main.o' 的配方失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48879053/