简体   繁体   English

C ++中的函数调用问题

[英]problem with function call in c++

I have a problem with my program, I work actually on interpolation with Cubic BSplines, but when I call my interpolate function in main() I get a wrong result, and when I write directly the function in main() it works :(. I can not see what is the difference between the both. 我的程序有问题,我实际上使用Cubic BSplines进行插值,但是当我在main()中调用插值函数时,会得到错误的结果,而当我直接在main()中编写该函数时,它是:(。我看不出两者之间有什么区别。

void CubicBSpline::interpolation(){
  Point3d point;
  for(unsigned int i = 3; i < (knots->m_points).size(); i++){
    for(double t=0; t<1; t+=0.1){
      point = bSplineCubicUniform(i, t);
      cout << point.x << " " << point.y <<endl;
    }
  }
}

int main(){
  CubicBSpline cbs(4, 4);
  cbs.interpolation(); //this is the call of my function but I got a wrong result

  // and here I write directly my function and that's work good
  Point3d point;
  for(unsigned int i = 3; i < (cbs.knots->m_points).size(); i++){
    for(double t=0; t<1; t+=0.1){
      point = cbs.bSplineCubicUniform(i, t);
      cout << point.x << " " << point.y <<endl;
    }
  }
  return 0;
}

Your description is decidedly uninformative (and it seems you'd rather respond with abrasive comments than provide more information) so all I can give you is a general answer. 您的描述绝对没有信息性(似乎您宁愿用粗糙的评论来回答而不是提供更多信息),所以我能给您的是一个一般性的答案。 A bunch of things could be different between these two cases. 这两种情况之间的一堆事情可能有所不同。 Firstly presumably these two functions are defined within different files therefore depending on what's included different types could have entirely different definitions. 首先,大概这两个函数是在不同的文件中定义的,因此根据所包含的内容,不同的类型可能具有完全不同的定义。 From what I can see you don't have any scoping problems, but maybe I'm wrong. 据我所知,您没有任何范围界定问题,但也许我错了。 And also since you don't provide us with code for these other functions who knows what side effects they have. 而且由于您没有为我们提供其他功能的代码,这些其他功能知道它们有什么副作用。

However honestly I think that your problem most likely is due to an optimization that the compiler makes when the function is inlined. 但是老实说,我认为您的问题很可能是由于函数内联时编译器进行了优化。 (Try compiling without optimizations). (尝试在不进行优化的情况下进行编译)。

Finally the most glaringly bad thing here is that you have a loop that uses a double as its iterator... are you kidding me? 最后,最明显的坏处是,您有一个循环使用double作为其迭代器……您在跟我开玩笑吗? Who knows how many times that loop get's executed. 谁知道执行该循环多少次。 It's entirely dependent on the compiler's floating point round off, which means not only is it unpredictable, but even if you get lucky and it works on your machine, who knows if it will work on other ones. 它完全取决于编译器的浮点数舍入,这意味着它不仅不可预测,而且即使您很幸运并且可以在您的计算机上运行,​​谁知道它是否可以在其他计算机上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM