【问题标题】:Error while declaring constructor in an Inherited class in c++在 C++ 中的继承类中声明构造函数时出错
【发布时间】:2020-08-13 23:17:35
【问题描述】:

我有两个类,Person 和 Student,其中 Person 是基类,Student 是派生类。不允许对 Person 类或 main 函数进行任何更改。请注意,Student 继承了 Person 的所有属性。一个学生类构造函数,它有参数:一个字符串,名字一个字符串,姓一个整数,id。测试分数的整数数组(或向量), 。 char calculate() 方法,计算学生对象的平均值并返回代表其计算平均值的成绩字符。样本输入 -

Heraldo Memelli 8135627 2 100 80

预期输出-

Name: Memelli, Heraldo ID: 8135627 Grade: O

我得到的错误是在声明构造函数时,你能解释一下为什么吗?你会建议其他方法吗?提前致谢。这是我的代码-

    #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 identification, vector < int > & scores) {
        for (int i = 0; i < & scores.size(); i++)
          this - > testScores.pushback( & scores[i]);
      }
      char calculate() {
        int avg, sum = 0, count = 0;
        for (int i = testScores.begin(); i < testScores.size(); i++) {
          sum = sum + testScores[i];
          count++;
        }
        avg = sum / count;
        if (avg >= 90 && avg <= 100)
          return ('O');
        else if (avg >= 80 && avg < 90)
          return ('E');
        else if (avg >= 70 && avg < 80)
          return ('A');
        else if (avg >= 55 && avg < 70)
          return ('P');
        else if (avg >= 40 && avg < 55)
          return ('D');
        else if (avg0 < 40)
          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;
    }

【问题讨论】:

  • 你得到了什么确切的逐字错误?将其添加到您的问题中。
  • 请分享错误信息。您在 Student 循环构造函数中有错字。 &amp;scores.size() 是什么?
  • 我不确定- &gt; 是编写运算符-&gt; 的有效方式。如果是的话,至少是非常不寻常的。

标签: c++ inheritance vector constructor ctor-initializer


【解决方案1】:

Person没有默认构造函数,所以你必须在mem-initializer中类Student的构造函数中显式调用带有Person类型子对象参数的构造函数列表。

构造函数可以如下所示

Student( const std::string &firstName, 
         const std::string &lastName, 
         int identification, 
         const std::vector<int> &scores ) 
         : Person( firstName, lastName, identification ), testScores( scores )
{
} 

并且成员函数可以这样定义

  char calculate() const
  {
    long long int sum = 0;

    for ( const auto &item : testScores )
    {
        sum += item;
    }

    long long int avg = testScores.size() == 0 ? 0 : sum / testScores.size();

    char c;

    if ( avg >= 90 )
      c = 'O';
    else if ( avg >= 80 )
      c = 'E';
    else if ( avg >= 70 )
      c ='A';
    else if ( avg >= 55 )
      c = 'P';
    else if ( avg >= 40 )
      c = 'D';
    else
      c = 'T';

    return c;
  }

至于你的代码,例如这个循环

    for (int i = 0; i < & scores.size(); i++)
      this - > testScores.pushback( & scores[i]);

无效,至少不应编译,因为您试图获取成员函数size 返回的右值的地址。

【讨论】:

    【解决方案2】:

    我认为很明显,您希望您的 Student 对象是一个具有给定名称等的人,但是您在代码中的什么地方这么说? firstNamelastNameidentitfication 参数未使用。这是你应该如何去做的

    Student(string firstName, string lastName, int identification, vector < int > & scores) : 
        Person(firstName, lastName, identification) {
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多