有人能告诉我为什么即使参数是正确的我也会收到此错误?

时间:2016-08-09 19:51:42

标签: c++ c++11

这是我试图提交的.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) <<"  "<< " | ";

    }
}

1 个答案:

答案 0 :(得分:2)

此日志表示在文件M2CourseTester.cpp中第19行和第25行使用错误的参数集调用类ICTCourse的构造函数。

可能有类似的代码:

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

虽然构造函数需要5个参数:

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

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

UPD。
以下语法不是默认参数:

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

您可以阅读有关默认参数here

的信息

看起来像这样:

//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