ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
You've created a global instance of the equation class called "quad", but you're getting errors because
you're referencing this class using the class name itself and not the instance. To cite an example:
line #111 reads:
Code:
result = equation.solve(0);
This causes a compilation error because equation is the name of the class, not an instance of that class.
Changing equation to quad (the name of your instance) fixes this:
Code:
result = quad.solve(0);
You must make this change to the following lines in order for the code to compile: 94, 98, 111, 115, 125
Also, I'm not sure which compiler you're using, but lines 63 and 67 will not compile under ANSI standard (g++ and other sane compilers will reject it. I don't know about microsoft, they use their own "standard".) If those lines are giving you errors, too, you need to add (double) before x2 in each function call to pow(). (Because there is no pow(int, int) function in ANSI! There is, however a pow(double, int)).
Also, there is a slight typo which will prevent the program from linking: you define the function prototype for "quadratic" but the later occuring definition itself uses the spelling "quadtratic". Change "quadtratic" to "quadratic" and you'll fix that error.
After making those changes, it will compile. I've tested that code under g++ after making the previously described changes and it compiles flawlessly. I don't know if there's any logic errors, though... I just quickly went through it and fixed the problems.
IN SUMMARY:
Lines 94, 98, 111, 115, 125: Change equation to quad
Line 106: Change "quadtratic" to "quadratic"
Lines 63, 67: Insert (double) before x2 in the pow(...) function call.
I don't think it liked the double being placed there, because the G++ (What I was using) returned this:
Code:
./calcCPP.cpp: In member function `float equation::solve(int)':
./calcCPP.cpp:63: error: expected primary-expression before "double"
./calcCPP.cpp:67: error: expected primary-expression before "double"
Hmmm... Well, copying and pasting EXACTLY what you posted and then compiling it gives me the following errors:
Code:
test.cpp: In member function `float equation::solve(int)':
test.cpp:63: error: call of overloaded `pow(int&, int)' is ambiguous
/usr/include/bits/mathcalls.h:154: error: candidates are: double pow(double,
double)
/usr/include/c++/3.3.4/cmath:512: error: long double
std::pow(long double, int)
/usr/include/c++/3.3.4/cmath:508: error: float std::pow(float,
int)
/usr/include/c++/3.3.4/cmath:504: error: double
std::pow(double, int)
/usr/include/c++/3.3.4/cmath:495: error: long double
std::pow(long double, long double)
/usr/include/c++/3.3.4/cmath:486: error: float std::pow(float,
float)
test.cpp:67: error: call of overloaded `pow(int&, int)' is ambiguous
/usr/include/bits/mathcalls.h:154: error: candidates are: double pow(double,
double)
/usr/include/c++/3.3.4/cmath:512: error: long double
std::pow(long double, int)
/usr/include/c++/3.3.4/cmath:508: error: float std::pow(float,
int)
/usr/include/c++/3.3.4/cmath:504: error: double
std::pow(double, int)
/usr/include/c++/3.3.4/cmath:495: error: long double
std::pow(long double, long double)
/usr/include/c++/3.3.4/cmath:486: error: float std::pow(float,
float)
test.cpp: In function `int main()':
test.cpp:94: error: parse error before `.' token
test.cpp:98: error: parse error before `.' token
test.cpp: In function `float quadtratic(int)':
test.cpp:111: error: parse error before `.' token
test.cpp:115: error: parse error before `.' token
test.cpp: In function `float median(float, float)':
test.cpp:125: error: parse error before `.' token
After adding the (double) or replaceing "using namespace std;" the errors are reduced to:
Code:
test.cpp: In function `int main()':
test.cpp:94: error: parse error before `.' token
test.cpp:98: error: parse error before `.' token
test.cpp: In function `float quadtratic(int)':
test.cpp:111: error: parse error before `.' token
test.cpp:115: error: parse error before `.' token
test.cpp: In function `float median(float, float)':
test.cpp:125: error: parse error before `.' token
And after making the remaining changes, no errors are reported.
I've never seen the "primary-expression" error you report from g++ before, although it resembles the "parse error" I got when I compiled your code.
Last edited by zhangmaike; 01-13-2005 at 06:44 PM.
I just double-checked, and apparently pow(int, int) is fine ANSI-wise... but there are still errors on those lines. The source of that problem is the "using namespace std;" line. If you replace that with "using std::cout;", "using std::cin;", and "using std::endl;" you'll be fine there.
Yeah. The function 5x^2 + 2x + 3 has no real zeroes. Try solving an equation yourself, first, then test the program to see if you get the same answer. And make sure your equation has real zeroes.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.