C++ 没有对错误的匹配函数调用

C++ No Matching function call to Errors

我有一个 class 计算两个具有质量、速度、位置和半径的圆形物体碰撞后的碰撞时间和速度。 Collision class 应该使用 Stone Class 来传递所有需要的信息;但是,它抛出一个不匹配的函数调用。

error: no matching function for call to ‘Stone::Stone()’ ision(double timein, string s1in, string s2in, map collection){ ^ physics_sim.cpp:27:14: note: candidate: Stone::Stone(double, std::pair, std::pair, double) explicit Stone(double radiusin, pair posin, pair velin, double massin){ ^~~~~ physics_sim.cpp:27:14: note: candidate expects 4 arguments, 0 provided physics_sim.cpp:20:7: note: candidate: constexpr Stone::Stone(const Stone&) class Stone{

class Stone {
private:
double radius;
pair<double, double> pos;
pair<double, double> vel;
double mass;
public:
explicit Stone(double radiusin, pair<double, double> posin, pair<double, double> velin, double massin){
    radius = radiusin;
    pos = posin;
    vel = velin;
    mass = massin;
}
double radiusGet(){
    return radius;
}
pair<double, double> velGet(){
    return vel;
}
pair<double, double> posGet(){
    return pos;
}
double massGet(){
    return mass;
} };

class Collision{
private:
double dotProduct(pair<double, double> a = {0, 0}, pair<double, double> b = {0,0}){
    return (a.first * b.first + a.second * b.second);
}
pair<double, double> subtract(pair<double, double> a = {0, 0}, pair<double, double> b = {0, 0}){
    pair<double, double> value = {a.first - b.first, a.second - b.second};
    return value;
}
double time;

Stone s1;
Stone s1new;
Stone s2;
Stone s2new;
public:
string s1name;
string s2name;
explicit Collision(double timein, string s1in, string s2in, map<string, Stone> collection){
    time = timein;
    s1 = collection.at(s1in);
    s1name = s1in;
    s2name = s2in;
    s2 = collection.at(s2in);
    pair<double, double> newPos;
    pair<double, double> newVel;
    pair<double, double> vdif1 = subtract(s1.velGet(),s2.velGet());
    pair<double, double> vdif2 = subtract(s2.velGet(),s2.velGet());
    pair<double, double> rdif1 = subtract(s1.radiusGet(),s2.radiusGet());
    pair<double, double> rdif2 = subtract(s2.radiusGet(),s1.radiusGet());

    newVel = {
        (s1.velGet().first - (2 * s2.massGet() * dotProduct(vdif1, rdif1) * rdif1.first / dotProduct(rdif1, rdif1) /(s2.massGet() + s1.massGet()) )),
        (s1.velGet().second - (2 * s2.massGet() * dotProduct(vdif1, rdif1) * rdif1.second / dotProduct(rdif1, rdif1) /(s2.massGet() + s1.massGet()) )),
    }
    newPos = {s1.posGet().first + s1.velGet().first * time, s1.posGet().second + s1.velGet().second * time};
    s1new = Stone(s1.radiusGet(), newVel, newPos, s1.massGet());
    newVel = {
        (s2.velGet().first - (2 * s1.massGet() * dotProduct(vdif2, rdif2) * rdif2.first / dotProduct(rdif2, rdif2) /(s2.massGet() + s1.massGet()) )),
        (s2.velGet().second - (2 * s1.massGet() * dotProduct(vdif2, rdif2) * rdif2.second / dotProduct(rdif2, rdif2) /(s2.massGet() + s1.massGet()) )),
    }
    newPos = {s2.posGet().first + s2.velGet().first * time, s2.posGet().second + s2.velGet().second * time};
    s2new = Stone(s2.radiusGet(), newVel, newPos, s2.massGet());
}
Stone s1Get()
    return s1;
Stone s2Get()
    return s2;
Stone s1newGet()
    return s1new;
Stone s2newGet()
    return s2new;
double timeGet()
    return time; };

no matching function错误是由于您没有定义默认构造函数造成的。

当您创建 class 时,默认构造函数和其他方法由编译器定义。但是,当您定义带参数的构造函数时,默认构造函数不再默认生成,您必须手动定义它。

在执行 class 的构造函数主体之前,会为 class 的所有成员调用默认构造函数。也就是说 Stone::Stone() 将在执行 Collision::Collision(...) 之前调用来初始化 s1s2,并且由于它没有找到默认构造函数,所以会出现该错误。