LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-04-2011, 05:45 AM   #1
ed88
LQ Newbie
 
Registered: Apr 2008
Posts: 13

Rep: Reputation: 0
Unhappy C++ and sqrt() problem


Hello to all, not much time ago i start to learnt c++ whitout teachers only whit books and tutorials ...
yestarday i had a problem whit a simple code and i don`t understand the problem, and how to fix it here my code :

Code:
#include <iostream> 
#include <math.h> 
using namespace std; 
double sq = sqrt(3); 

int main () 
{ 
double s, p, a; 
cout << "p of triangle : \n"; 
cin >> p; 
a = p / 3; 
s = (a * a * sq) / 4; 
cout << "s of triangle e : " << s << "\n"; 
return 0; 
}
Here is the error that i get if is posible to tell me how to fix it and evade it in the future :
IntelliSense: more than one instance of overloaded function "sqrt" matches the argument list: d:\visual studio 2010\projects\a\a\a.cpp 4 13 a

P.S: I use visual studio 2010
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-04-2011, 06:02 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
First of all, it is spelt "with", not "whit"; similarly, it is "without", not "whitout".

In your little program, and in fact in most, there is very little need to for a global variable. Thus I would recommend that you declare sq within the main() function. Also, the API for sqrt() indicates that it accepts a double-value for the parameter; you are passing an int-value. Try:
Code:
int main()
{
   const double sq = sqrt((double) 3);
   ...
}
 
3 members found this post helpful.
Old 12-04-2011, 07:26 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by ed88 View Post
more than one instance of overloaded function "sqrt"
dwhitney67 gave you most of the answer. I'll add just a little more.

You can use an integer in place of a double anywhere the compiler knows a double is required. For example, elsewhere in your program you have:
Code:
a = p / 3
That 3 needs to be a double. You provided an int. The compiler understands all that and automatically casts the int to a double.
If the only available definition of sqrt(x) required x to be a double, then sqrt(3) would work just fine. The compiler would cast the int to a double.
But there is some other definition of sqrt available, maybe sqrt(std::complex<double>). I don't recall exactly what math.h brings in and also Visual Studio header files often bring in more than they are supposed to.
Int can be cast to the input type of more than one overload of sqrt, so the compiler doesn't know which sqrt() you mean.

Last edited by johnsfine; 12-04-2011 at 07:30 AM.
 
1 members found this post helpful.
Old 12-04-2011, 07:52 AM   #4
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by ed88 View Post
Code:
using namespace std;
Remove "using namespace std" - it is a very bad habit.
Put "using std::cout;" into main() instead of that:
Code:
int main(int argc, char** argv){ 
    using std::cout; 
    //...
}


Quote:
Originally Posted by dwhitney67 View Post
[code]
int main()
{
const double sq = sqrt((double) 3);
...
}
I'd recommend
Code:
int main(){
   const double sq = sqrt(3.0);
   ...
}
"3" is an int.
"3.0" - double.
"3.0f" - float.

Quote:
Originally Posted by johnsfine View Post
I don't recall exactly what math.h brings in
In such situation cl.exe (visual studio compiler, obviously) normally lists possible candidates within compiler output right under error message:
Code:
error C2668: 'sqrt' : ambiguous call to overloaded function
	math.h(581): could be 'long double sqrt(long double)'
	math.h(533): or       'float sqrt(float)'
	math.h(128): or       'double sqrt(double)'
        while trying to match the argument list '(int)'
 
1 members found this post helpful.
Old 12-07-2011, 08:24 AM   #5
ed88
LQ Newbie
 
Registered: Apr 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks of all
if (some one have someting to add)
will be nice
else
Thanks again

Last edited by ed88; 12-07-2011 at 08:26 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sqrt function lmvent Programming 6 10-20-2005 07:53 PM
Problems with Sqrt() nostrum Programming 2 04-15-2005 12:24 PM
How do you write your own sqrt()? The_Nerd Programming 6 11-02-2004 08:08 PM
Bash sqrt problem Dimitris Programming 1 08-26-2003 03:30 PM
sqrt() function IamDaniel Programming 6 08-09-2003 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration