【问题标题】:C++ No Matching function call to Errors [closed]C ++没有对错误的匹配函数调用[关闭]
【发布时间】:2018-05-03 06:40:18
【问题描述】:

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

错误:没有匹配函数调用‘Stone::Stone()’ ision(double timein,字符串 s1in,字符串 s2in,地图集合){ ^ physics_sim.cpp:27:14: 注意: 候选: Stone::Stone(double, std::pair, std::pair, double) 显式 石头(双半径,对 posin,对 velin,双质量){ ^~~~~physics_sim.cpp:27:14:注意: 候选人需要 4 个参数,0 提供了physics_sim.cpp:20:7: 注意: 候选: 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; };

【问题讨论】:

  • 您能否重新格式化代码并提供确切的错误信息?
  • 确切消息是什么?什么线?请减少代码示例中的干扰,将其设为minimal reproducible example,以便我们更好地帮助您。
  • 错误:没有匹配函数调用'Stone::Stone()' ision(double timein, string s1in, string s2in, map collection){ ^ Physics_sim.cpp:27 :14: 注意: 候选: Stone::Stone(double, std::pair, std::pair, double) 显式 Stone(double radiusin, pair posin, pair velin, double massin){ ^~~~~ Physics_sim.cpp:27:14: 注意:候选人需要 4 个参数,0 提供了 Physics_sim.cpp:20:7: 注意:候选人:constexpr Stone:: Stone(const Stone&) class Stone{

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


【解决方案1】:

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

当您创建一个类时,编译器会定义一个默认构造函数以及其他方法。但是,当你定义一个带参数的构造函数时,默认的构造函数就不再默认生成了,你必须手动定义它。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    相关资源
    最近更新 更多