有人能告诉我为什么我得到这个错误,即使参数是正确的

Can someone tell me why am i getting this error even though the arguments are correct?

本文关键字:参数 错误 告诉我 为什么      更新时间:2023-10-16

这是我试图提交的。h文件,由于相同类型的错误,测试人员不让它通过。

#ifndef SICT_ICTCOURSE_H__
#define SICT_ICTCOURSE_H__
#include "Course.h"
namespace sict {
    class ICTCourse : public Course {
    private:
        char computerSystem_[6 + 1];
    public:
        ICTCourse();
        ICTCourse(const char* courseCode, const char* coursetitle, int credits, int studyload, char* computerSystem);
        const char* getComputerSystem() const;
        void setComputerSystem(const char* value);
        void display(ostream&);
    };
}
#endif

这些是我不明白的错误。我已经提供了所有需要的参数。

M2CourseTester.cpp: In function âbool isEmptyTest1()â:
M2CourseTester.cpp:19:38: error: no matching function for call to    âsict::ICTCourse::ICTCourse(const char [1], const char [6], int, int)â
M2CourseTester.cpp:19:38: note: candidates are:
ICTCourse.h:20:3: note: sict::ICTCourse::ICTCourse(const char*, const char*, int, int, char*)
ICTCourse.h:20:3: note:   candidate expects 5 arguments, 4 provided
ICTCourse.h:18:3: note: sict::ICTCourse::ICTCourse()
ICTCourse.h:18:3: note:   candidate expects 0 arguments, 4 provided
ICTCourse.h:9:8: note: sict::ICTCourse::ICTCourse(const sict::ICTCourse&)
ICTCourse.h:9:8: note:   candidate expects 1 argument, 4 provided
M2CourseTester.cpp: In function âbool isEmptyTest2()â:
M2CourseTester.cpp:25:37: error: no matching function for call to âsict::ICTCourse::ICTCourse(const char [5], const char [1], int, int)â
M2CourseTester.cpp:25:37: note: candidates are:
ICTCourse.h:20:3: note: sict::ICTCourse::ICTCourse(const char*, const char*, int, int, char*)
ICTCourse.h:20:3: note:   candidate expects 5 arguments, 4 provided
ICTCourse.h:18:3: note: sict::ICTCourse::ICTCourse()
ICTCourse.h:18:3: note:   candidate expects 0 arguments, 4 provided
ICTCourse.h:9:8: note: sict::ICTCourse::ICTCourse(const sict::ICTCourse&)  
ICTCourse.h:9:8: note:   candidate expects 1 argument, 4 provided

这是ICTCourse.cpp文件

#include "ICTCourse.h"
namespace sict {
    ICTCourse::ICTCourse():Course() {
        strcpy(computerSystem_, "matrix");
    }
    ICTCourse::ICTCourse(const char* courseCode, const char* courseTitle, int credits, int studyLoad, char* computerSystem){
        courseCode_Setter(courseCode);
        courseTitle_Setter(courseTitle);
        credits_Setter(credits);
        studyLoad_Setter(studyLoad);
        strcpy(computerSystem_, computerSystem);
    }
    const char* ICTCourse::getComputerSystem() const{
        return computerSystem_;
    }
    void ICTCourse::setComputerSystem(const char* value){
        strncpy(computerSystem_, value, 6);
    }
    void ICTCourse::display(ostream& pout){
        pout << left << setw(MAX_COURSECODE_LEN) << getCourseCode() << " | " << left << setw(20) << getCourseTitle() << " | " << right << setw(6) << getCredits() << " | " << right << setw(4) << getStudyLoad() << " | " << left << setw(6) << computerSystem_ << " | " << setw(4) <<"  "<< " | ";
    }
}

这个日志说,在文件M2CourseTester.cpp第19行和第25行,类ICTCourse的构造函数被调用了一组错误的参数。

可能有类似这样的代码:

ICTCourse* course = new ICTCourse("", "maths", 1, 1);

构造函数需要5个形参:

  • const char [5]const char* courseCode - ""
  • const char [1]const char* coursetitle - "maths"
  • int credits - 1类型int
  • int studyload - 1类型int
  • char* computerSystem - missing

你需要为构造函数提供第五个参数。



乌利希期刊指南。以下语法不是默认参数:

ICTCourse::ICTCourse()
  :Course() {
    strcpy(computerSystem_, "matrix");
}

你可以在这里阅读默认参数

它看起来像这样:

//ICTCourse.h
...
ICTCourse(const char* courseCode,  
  const char* coursetitle,  
  int credits, 
  int studyload,  
  char* computerSystem = "matrix");
  //                   ^^^^^^^^^^ default value here
...
//ICTCourse.cpp
...
ICTCourse::ICTCourse(const char* courseCode,  
  const char* courseTitle,  
  int credits,  
  int studyLoad,  
  char* computerSystem /* = matrix*/){
  //                   ^^^^^^^^^^ no value here
  //                   good practice to keep it as a comment