Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
At school I'm in AP Computer Science and one of our projects in java was to make a program with the quadratic equation. That wasn't too hard in java. So I decided to do it in C++ for fun today and here is my code:

Code:
#include "stdafx.h"
#include <iostream>
#include <math.h>

using namespace std;

class QuadraticEquation
{
public:
   class QuadraticEquation(double a1, double a2, double a3){
      a = a1;
      b = a2;
      c = a3;
   };
   bool hasSolutions(){
      if (solution1>0) {return true;}
      else {return false;}
   };
   double getSolution1(){
   solution1 = (-b+(sqrt((b*b)-(4*a*c))))/2;
   solution2 = (-b-(sqrt((b*b)-(4*a*c))))/2;
   if(solution1>solution2) {return solution1;}
   else {return solution2;}
   };
   double getSolution2(){
   solution1 = (-b+(sqrt((b*b)-(4*a*c))))/2;
   solution2 = (-b+(sqrt((b*b)-(4*a*c))))/2;
   if(solution1>solution2) {return solution2;}
   else {return solution1;}
   };
private:
double a;
double b;
double c;
double solution1;
double solution2;
};

int main()
{
   
   QuadraticEquation quad(1,-2,-17);
   cout<<quad.getSolution1();
   cout<<quad.getSolution2();
   cout<<quad.hasSolutions();
   cin.get();
   return 0;
}


The only error I got from this was saying:
"1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Programming\C++\Projects\quad2\Debug\quad2.exe : fatal error LNK1120: 1 unresolved externals"

Can anyone explain what i'm doing wrong?
You seem to have a nested definition of QuadraticEquation; what's that about? If you're trying to define the constructor, you don't prefix it with the keyword class. Smile
yep, I don;t think you should be having the prefix class, other than that I cant see any other errors ^.^
Which "class" should I not have? if i take the "class" out of "class QuadraticEquation(double a1, double a2, double a3)" it still says unresolved externals. If i take it out of the first one, it just gives me like 15 errors
AbsoluteProgram wrote:
Which "class" should I not have? if i take the "class" out of "class QuadraticEquation(double a1, double a2, double a3)" it still says unresolved externals. If i take it out of the first one, it just gives me like 15 errors
This one:
Code:
   class QuadraticEquation(double a1, double a2, double a3){
      a = a1;
      b = a2;
      c = a3;
   };
. The constructor returns a class anyway; you don't need to specify that. Smile The declaration should be fine if you do that...
I took out that one and it still gave the same error.
AbsoluteProgram wrote:
I took out that one and it still gave the same error.
Also, why are you ending your member function definitions with semicolons inside the class definition? I'm surprised that that even compiles.
and why do you define double a1 and then define a1 as a?

it seems unnessacary, but then again, I am more of a C# person >.>
so I am using that knowledge while looking at this :/
qazz42 wrote:
and why do you define double a1 and then define a1 as a?

it seems unnessacary, but then again, I am more of a C# person >.>
so I am using that knowledge while looking at this :/
a, b, and c are the member fields of the class, while a1, b1, and c1 are arguments to the constructor to set the initial values.
The error you're getting seems like a linking error. What compiler are you using? Visual Studio I'm assuming.
You're using project settings for a Windows GUI application (hence the linker is trying to resolve WinMain and failing as you haven't provided one). Click Project, Properties, Configuration Properties, Linker, System and set SubSystem to Console instead of Windows.
benryves wrote:
You're using project settings for a Windows GUI application (hence the linker is trying to resolve WinMain and failing as you haven't provided one). Click Project, Properties, Configuration Properties, Linker, System and set SubSystem to Console instead of Windows.
Ooooh, good call. That's what I get for not examining the actual error closely enough. Sad
benryves wrote:
You're using project settings for a Windows GUI application (hence the linker is trying to resolve WinMain and failing as you haven't provided one). Click Project, Properties, Configuration Properties, Linker, System and set SubSystem to Console instead of Windows.


That fixed it, thank you Very Happy

And
KermMartian wrote:
Also, why are you ending your member function definitions with semicolons inside the class definition? I'm surprised that that even compiles.

I saw someone do it in a youtube video while trying to figure out what my problem was so I tried that and never changed it back after it didn't help my problem
You end a class definition or a struct definition with a curly brace and a semicolon, but function/methods end in a simple curly brace. Smile
Ohh alright thank you Smile

On
Code:
bool hasSolutions(){
      if (solution1>0) {return true;}
      else {return false;}
   }

When I run it, it says "1" instead of "true". any idea why?
C++'s "true" is an integer with a value of 1 and "false" is an integer with a value of 0. You would need to convert these to a string yourself (hasSolutions() ? "true" : "false", for an example using the conditional operator).
benryves wrote:
C++'s "true" is an integer with a value of 1 and "false" is an integer with a value of 0. You would need to convert these to a string yourself (hasSolutions() ? "true" : "false", for an example using the conditional operator).
Exactly this. It would never interpret even the boolean values true and false as the strings "true" and "false" unless you instruct it how to do so.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement