gpt4 book ai didi

c++ - 有人能告诉我为什么我会收到这个错误,即使参数是正确的吗?

转载 作者:行者123 更新时间:2023-11-28 02:02:32 25 4
gpt4 key购买 nike

这是我正在尝试提交的 .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* courseCode - "" 类型 const char [5]
  • const char* courstitle - "maths" 类型 const char [1]
  • int credits - 1 类型 int
  • int studyload - 1 类型 int
  • char* computerSystem - 缺少

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


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

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

关于c++ - 有人能告诉我为什么我会收到这个错误,即使参数是正确的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38859308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com