C ++ No Matching函数调用错误

时间:2018-05-03 06:40:18

标签: c++ class object-oriented-analysis

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

  

错误:没有匹配函数来调用'Stone :: Stone()'ision(double   timein,string s1in,string s2in,map collection){^   physics_sim.cpp:27:14:注意:候选人:石头::石头(双,   std :: pair,std :: pair,double)显式   石(双半径,对posin,对velin,双massin){^ ~~~~ physics_sim.cpp:27:14:注意:   候选人需要4个参数,0提供physics_sim.cpp:20:7:注意:   候选人:constexpr Stone :: Stone(const Stone&)类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; };

1 个答案:

答案 0 :(得分:0)

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

创建类时,编译器会定义默认构造函数以及其他方法。但是,当您使用参数定义构造函数时,默认情况下不再生成默认构造函数,您必须手动定义它。

在执行该类的构造函数体之前,将为类的所有成员调用默认构造函数。在Stone::Stone()执行之前,s1将被调用以初始化s2Collision::Collision(...),并且由于找不到默认构造函数,因此会出现该错误。