LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 02-17-2008, 04:25 PM   #1
harkonen
Member
 
Registered: May 2007
Distribution: slackware
Posts: 37

Rep: Reputation: 15
noob C++ compiling issues


My professor uses Windows only and can't help me, so I'm hoping someone can explain this. I think it must be a really easy noob question, so probably you can.

Really simple C++ programs compile easily with g++

But anything with more than just an <iostream> header gives me problems. Example:

Quote:
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
double u, v;
string str;

cout << "2 to the power of 6 = " << pow(2, 6) << endl;
u = 12.5;
v = 3.0;
cout << "And " << u << " to the power of " << v << " = " << pow(u, v) << endl;

cout << "Square root of 24 = " << sqrt(24.0) << endl;
u = pow(8.0, 2.5);
cout << "u = " << u << endl;
str = "Programming with C++";
cout << "Length of str = " << str.length() << endl;

return 0;
}
returns this when I try to compile it (this is pasted in from my Fedora laptop, but it gives the same result on Slackware and Ubuntu laptops):

Quote:
[sXe@localhost]~/Documents/c_class% gcc -o power pow.cpp

pow.cpp: In function ‘int main()’:
pow.cpp:12: error: call of overloaded ‘pow(int, int)’ is ambiguous
/usr/include/bits/mathcalls.h:154: note: candidates are: double pow(double, double)
/usr/lib/gcc/ppc64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/cmath:361: note: long double std:ow(long double, int)
/usr/lib/gcc/ppc64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/cmath:357: note: float std:ow(float, int)
/usr/lib/gcc/ppc64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/cmath:353: note: double std:ow(double, int)
/usr/lib/gcc/ppc64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/cmath:349: note: long double std:ow(long double, long double)
/usr/lib/gcc/ppc64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/cmath:345: note: float std:ow(float, float)
The program is straight out of my textbook so I'm sure the syntax must be right, I am assuming I either don't have my laptop configured correctly for C++ programming or I'm not up to date or I'm not using a flag with g++ that I should be using.

THanks in advance for help.
 
Old 02-17-2008, 05:59 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
Hi-

It's correct for C ... but ambiguous for C++. Try this:
Quote:
cout << "2 to the power of 6 = " << pow(2.0, 6.0) << endl;
'Hope that helps .. PSM
 
Old 02-17-2008, 06:19 PM   #3
deejxue
LQ Newbie
 
Registered: Feb 2008
Posts: 4

Rep: Reputation: 0
Never assume because it comes out of a book its correct...doesn't help us when we're studying tho, right? Anyways....

First, you have to define the values for your variables before you do an output statement so you need to move the values for u and v BEFORE the cout statement that calls the variables otherwise it appears undefined.

Second, the first cout statement uses two values that are undefined. The way they are without any decimal point says to the compiler that you are using 2 integers. According to the POW function library, here are your options (copied and pasted here from the following link
http://en.allexperts.com/q/C-1040/fu...gar-string.htm) :

C++ (taken from book "The C++ Standard"):

double pow(double x, double y);
double pow(double x, int y);
float pow(float x, float y);
float pow(float x, int y);
long double pow(long double x, long double y);
long double pow(long double x, int y);

One of the numbers MUST be a floating point number. You can define it in a case like yours one of two ways (that I have discovered so far)
a) change one of your undeclared numbers to have a decimal point in it eg. pow(1.0, 2)
b) put double in the function eg. pow(double(2), 6)

Both seem to resolve the issue for me.

BTW, our class was to find out if pow would accept an integer or only double in the function. That's how I ended up finding out the rules for the pow library.

Hope it helps.
 
Old 02-17-2008, 06:55 PM   #4
harkonen
Member
 
Registered: May 2007
Distribution: slackware
Posts: 37

Original Poster
Rep: Reputation: 15
that fixed it

Thanks both of you. making it pow(6.0, 2.0) fixed it. The other information is helpful too. I've bookmarked that link, and maybe I'll check out the book "The C++ Standard". I'm really seeing that I need to find a Linux-friendly C++ book because the textbook we're using is clearly of the "if it works on Microsoft, it's good enough" school of thought... but I think I'd rather learn it the right way instead.

Thanks for the help!
 
Old 02-17-2008, 07:06 PM   #5
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
Hi, again -

Very simply, the *only* problem is that the Standard C Math library (from C++ header "<cmath>") doesn't happen to have an overloading for "pow (int, int)".

What you typed in originally is perfectly legitimate C. It even compiles and runs without error on an old GCC 2.96 compiler I happen to have laying around. But it *doesn't* compile on my new GCC 3.3.5 compiler - I get exactly the same error as you.

All I have to do is fix the single call I mentioned above.

Here's the working code:
Code:
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

int
main()
{
  double u, v;
  string str;

  cout << "2 to the power of 6 = " << pow(2.0, 6.0) << endl;
  u = 12.5;
  v = 3.0;
  cout << "And " << u << " to the power of " << v << " = " << pow(u, v) << endl;

  cout << "Square root of 24 = " << sqrt(24.0) << endl;
  u = pow(8.0, 2.5);
  cout << "u = " << u << endl;
  str = "Programming with C++";
  cout << "Length of str = " << str.length() << endl;

  return 0;
}
And here's the sample output:
Quote:
2 to the power of 6 = 64
And 12.5 to the power of 3 = 1953.12
Square root of 24 = 4.89898
u = 181.019
Length of str = 20
'Hope that helps .. PSM

PS:
Harkonen - I just noticed that our posts crossed each other, and that you resolved the problem - cool! And you're right - Microsoft has always been notorious about being sloppy with the C++ standard ... just as GCC 3.x can be a bit overzealous sometimes. Anyway - glad you're squared away!

Last edited by paulsm4; 02-17-2008 at 07:10 PM. Reason: OP already resolved problem
 
Old 02-18-2008, 01:42 AM   #6
deejxue
LQ Newbie
 
Registered: Feb 2008
Posts: 4

Rep: Reputation: 0
(My bad on the variables, I thought they were defined after the line they ran in. Must've copied it wrong in the editor.)

That would answer the question my instructor had about using INT instead of DOUBLE. I am new to C++, mostly do just simply shell scripting for automation, but taking this class as a requirement.

Interesting to know it worked in C. I do know that Micro$oft breaks everything, one of their favorite things to do, then make it work their way in their environments. Anyways, I'm glad to have learned from this thread also. I posted only because of the research I'd done on this and what I had discovered.

Nice to know there's a place to post and find answers to these questions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
complete noob with several issues in suse 10.0 animematt Linux - Newbie 1 07-02-2006 02:55 AM
Help for noob and kernel 2.6.14 compiling? masterross Slackware 2 01-04-2006 06:20 PM
Relative noob trying to resolve IRQ issues.. pinnocchio Linux - Hardware 3 12-01-2004 05:45 PM
Compiling - Noob question Sc0rp Linux - Newbie 5 09-25-2004 06:38 AM
total noob compiling error goddamn walrus Linux - Newbie 6 10-06-2003 05:45 PM


All times are GMT -5. The time now is 02:39 PM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration