KY90
2021-06-19 22:21
采纳率: 100%
浏览 86

没有与参数列表匹配的构造函数

#include<iostream>

#include<string.h>

using namespace std;

class Person {

public:

Person() {}

Person(const char* name, int id){

this->id = id;

strcpy_s(this->name, 81, name); }

~Person() {};

 

谁能帮我看看是哪里有问题吗?

void Display() {

cout << name << " " << id << " " << endl; }

protected:

char name[81];

int id;

};

class CollegeStu:public Person {

public:

CollegeStu() {}

CollegeStu(const char* major,double score):Person(name,id)

{ this->score =score ;

strcpy_s(this->major, 81, major);

void Display() {

cout <<name<<" "<<id<<" "<< major << " " << score << endl;

}

private:

char major[81];

double score;

};

int main()

{

char name[81], major[81];

int id;

double score;

cin >> name >> id >> major >> score;

CollegeStu cs(name, id, major, score);

cs.Display();

return 0;

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • qzjhjxj 2021-06-19 22:38
    已采纳

    修改如下,供参考,望采纳:

    #include<iostream>
    
    #include<string.h>
    
    using namespace std;
    
    class Person {
    public:
    
           Person() {}
    
           Person(const char* name, int id){
                   this->id = id;
                  strcpy(this->name, name);
           }
    
           ~Person() {};
           //谁能帮我看看是哪里有问题吗?
    
           void Display() {
               cout << name << " " << id << " " << endl;
           }
    
    protected:
    
           char name[81];
           int id;
    };
    
    class CollegeStu:public Person {
    public:
    
          CollegeStu() {}
          CollegeStu(const char *name,int id,const char* major,double score):Person(name,id)
          {
               this->score =score ;
               strcpy(this->major, major);
          }
    
          void Display() {
               cout <<name<<" "<<id<<" "<< major << " " << score << endl;
          }
    
    private:
    
          char   major[81];
          double score;
    };
    
    int main()
    
    {
        char name[81], major[81];
        int  id;
        double score;
        cin >> name >> id >> major >> score;
        CollegeStu cs(name, id, major, score);
        cs.Display();
        
        return 0;
    }
    点赞 评论
  • 缘来是梦 2021-06-19 22:27

    CollegeStu的构造函数只声明了3个参数,而在main函数里创建CollegeStu对象的时候却传了4个参数。

    点赞 评论

相关推荐 更多相似问题