我正在尝试创建两个简单的类,但这段代码由于某种原因无法编译。它说我调用 point::point 时没有参数,但我唯一一次调用 point::point 是在 main 函数中,我确实在那里调用了参数。
调试的时候发现是string_two_points的构造函数在调用point的构造函数。
#include <iostream>
#include <string>
using namespace std;
class point{
public:
int x;
int y;
point(int x, int y): x(x),y(y){};
point(const point & c): x(c.x), y(c.y) {};
};
class string_two_points{
public:
string type;
point x;
point y;
string_two_points(string type, point & xc):type(type){
x=xc;
};
string_two_points(string type, point & xc, point & yc):type(type){
x=xc;
y=yc;
};
};
int main(){
point a = point(2,3);
point b = point(3,4);
string_two_points x = string_two_points(string("abc"),a,b);
return 0;
}
最佳答案
string_two_points(string type, point & xc):type(type){
此构造函数未初始化 point y
,因此它通过调用默认无参数构造函数 point::point
(不存在)进行默认初始化。
关于c++ - 为什么这个构造函数不带参数调用另一个构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40942603/