c++ - 我收到 2 个我无法解决的错误; 1 无效使用成员(你忘记了 '&' 吗?)和目标 'main.o' 的配方失败

标签 c++

当我编译代码时,我在第 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/

相关文章:

c++ - 从非成员静态函数发出信号

c++:如何将字符串分解(不解析)为命令行参数?

c++ - C++中如何访问基类的成员变量?

c++读取数组结构问题

c++ - 使用 std::chrono 库来调整应用程序 fps 但出现奇怪的行为

c++ - map 不能用作 C++ 中的数组样式打印吗?

c++ - 使用 SFINAE 原理时重载函数有歧义

c++ - 线程池棒

c++ - 为什么while的条件在do while范围之外

c++ - OpenGL 到 DirectX(缓冲区到纹理到屏幕)