C++ C++;指向类的指针
我有一个关于类指针的问题。我的意思是在提供类中,我想创建一个C++ C++;指向类的指针,c++,class,pointers,C++,Class,Pointers,我有一个关于类指针的问题。我的意思是在提供类中,我想创建一个Course*sample指针,顺便说一下,Course也是另一个类,它接受一个int参数,然后通过sample=new Course尝试从内存中获取空间但我得到一个错误,上面写着: Offering.cpp:12:19: error: no matching function for call to ‘Course::Course()’ Offering.cpp:12:19: note: candidates are: lib.h:6
Course*sample
指针,顺便说一下,Course也是另一个类,它接受一个int参数,然后通过sample=new Course尝试从内存中获取空间代码>但我得到一个错误,上面写着:
Offering.cpp:12:19: error: no matching function for call to ‘Course::Course()’
Offering.cpp:12:19: note: candidates are:
lib.h:63:5: note: Course::Course(int)
lib.h:63:5: note: candidate expects 1 argument, 0 provided
有人知道我错在哪里吗?无论如何,谢谢。编写课程*sample=新课程(11)代码>
如果课程采用int
作为构造函数参数,则必须传递它,即使在堆上分配时也是如此。为课程定义默认构造函数。如果你不能,或者不应该,你将不得不使用现有的
Course * sample = new Course(1337);
您需要执行以下操作:
Course *sample = new Course(10);
这是因为您显然为课程定义了一个构造函数,其定义如下:
Course(int);
定义自定义构造函数时,不会自动添加默认构造函数(Course()) 没有代码,就无法提供帮助。但是你读过错误信息和它想告诉你什么吗?解决方法非常简单