Sign in to follow this  

[C++] sqrt()?

This topic is 3622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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>

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites
Quote:
Original post by that dude over there
I'll try re-coding the program and maybe change the sqrt to take int variables but return a float.

I've run across this once before, though I can't remember where, nor can I reproduce it. The problem was there was no overload for floats for some reason. Try casting the input to a double and the output back to a float:

float Dist(float x1,float x2,float y1,float y2)
{
return static_cast<float>(std::sqrt(static_cast<double>((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))));
}



[Edit: Also, post the full error, if there's any more to it, such as the list of possible functions.]

Share this post


Link to post
Share on other sites
Quote:
Original post by antimouse
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


sqrt assumes double, not int.

Share this post


Link to post
Share on other sites

This topic is 3622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this