[C++] sqrt()?

Started by
11 comments, last by Jerax 16 years, 3 months ago
Hello. I'm working on a point_distance code for use in a friend of mine's program. I've got the numbers figured out, but I can't get sqrt() to work. Here's my code...

#include <iostream>
#include <cmath>
float Dist(float x1,float x2,float y1,float y2);
int main()
{
  float x1,x2,y1,y2;
  std::cout<<"Enter x position 1: ";
  std::cin>>x1;
  std::cout<<"\nEnter y position 1: ";
  std::cin>>y1;
  std::cout<<"\nEnter x position 2: ";
  std::cin>>x2;
  std::cout<<"\nEnter y position 2: ";
  std::cin>>y2;
  float d=Dist(x1,x2,y1,y2);
  std::cout<<"\n\nDistance between points is: "<<d<<std::endl;
}
float Dist(float x1,float x2,float y1,float y2)
{
  float d=std::sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
  return d;
}
Ok, the main thing I'm unsure about is the "std::" in front of "sqrt()". Either way, this code doesn't work. The error message is "ambiguous call to overload function." Thanks for any help given. ~TDOT>
____________________________I'm 15, and beginning C#, and maybe Python. I'm fairly experienced in GML as well, and would be happy to help in GM related questions.
Advertisement
looking at the problem i cant really see one, the sqrt function should work fine, try building a new console application

also, the std:: you refer to is a namespace, if you put the code

using namespace std;


after your #include <iostream> declarationyou will not have to write std:: before everything
---Terence Burns---
Your code compiles fine for me on the compilers I tried. What compiler is giving you this problem?
Works fine for me under MSVC++ 2008 Express Edition, if that's any consolation.

Maybe try using <math.h> instead?
No problems here either (Visual Studio 08 Pro).

"ambiguous call to overload function.", means that a function has been defined to accept various different sets of arguments (overloaded), but the compiler can't tell which version of the function you are trying to use, because it could be any of them.

Can you give us a line number or a bit more information on the error you're getting?
I think i already saw this error before. Not every compiler identifies it. I think the reason is the variables types. The sqrt() function assumes you're providing an int variable, but you're working with floats. I'm not sure about it, but i think that's the mistake.

And use "cmath" instead of "math.h", as you did. In C++, "cmath" is the standard implementation for math operations.

Hope it helps.
[]s
Sorry, i said to use "cmath", but it's not a rule. Just prefer it against "math.h". Maybe sqrt() in "math.h" will work fine.
The standard library is post 99 C++ has deprecated the .h headers. Do not rely on the backward compatibility support. Use the modern standard library headers.
Wow, lots of replies! Ok, I'll try and answer everyone..
Quote:looking at the problem i cant really see one, the sqrt function should work fine, try building a new console application

also, the std:: you refer to is a namespace, if you put the code



using namespace std;



after your #include <iostream> declarationyou will not have to write std:: before everything

Yes, I know about the using namespace, but I prefer not to use it. It seems a long time ago when I was still looking for a language to try out, I ran into a topic where someone used that code and was told to try not to use it. They said either use using std::cout (etc.) or just write the std:: before every function.
I think I will try building a new application, just to see if it will work.

Quote:Your code compiles fine for me on the compilers I tried. What compiler is giving you this problem?
I'm using MSVC Express 2005. I'm also using Code::Blocks as my IDE. Is the new 2008 version free to download? Or only a trial version?

Quote:No problems here either (Visual Studio 08 Pro).

"ambiguous call to overload function.", means that a function has been defined to accept various different sets of arguments (overloaded), but the compiler can't tell which version of the function you are trying to use, because it could be any of them.

Can you give us a line number or a bit more information on the error you're getting?
Ok, so that explains the error. As for more information, the line is the line with the sqrt() function itself. Sorry, but that's all I can offer as far as that goes.

Quote:I think i already saw this error before. Not every compiler identifies it. I think the reason is the variables types. The sqrt() function assumes you're providing an int variable, but you're working with floats. I'm not sure about it, but i think that's the mistake.

And use "cmath" instead of "math.h", as you did. In C++, "cmath" is the standard implementation for math operations.

Hope it helps
Hmm, only ints? Well, maybe I can get by with only returning a float and using an int variable as the x and y coordinates. So <cmath> over <math.h> then? The only resaon I even know that the library existed is cause I searched for an answer to my problem (finding a sqare root) before posting my error. I was hoping that the header would work.

Quote:The standard library is post 99 C++ has deprecated the .h headers. Do not rely on the backward compatibility support. Use the modern standard library headers.
Noted. I've been trying to use only what's most recent as far as libraries go. If anyone knows of a list of standard libraries available, along with functions in those libraries, that would be extremely helpful.

Thanks to everyone for taking the time to help. I'll try re-coding the program and maybe change the sqrt to take int variables but return a float.

~TDOT>
____________________________I'm 15, and beginning C#, and maybe Python. I'm fairly experienced in GML as well, and would be happy to help in GM related questions.
Quote:Is the new 2008 version free to download? Or only a trial version?


The Express edition is free.

This topic is closed to new replies.

Advertisement