| rodsRisk (14) | |||
|
I am attempting to build a basic windows forms application that takes several parameters and calculates a price when the button is clicked. I am using the TryParse method to convert the input strings into doubles and implemented some functions to calculate the price of the option but I keep getting an error that says the following: "1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(271): error C2082: redefinition of formal parameter 'e' 1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(297): error C2601: 'callPrice' : local function definitions are illegal 1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(303): error C2601: 'N' : local function definitions are illegal 1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(325): error C2601: 'd1' : local function definitions are illegal 1>c:\users\rodrigo\documents\visual studio 2010\projects\optionscalc\optionscalc\Form1.h(330): error C2601: 'd2' : local function definitions are illegal" along with some other errors claiming that the identifiers for my variables are undeclared. My other issue is that I'm not 100% sure where to include the math library in my code i.e. at the top of all the visual code generated or right before my button click event. Below is my code...PLEASE HELP!
| |||
|
Last edited on
|
|||
| bobdabilder (56) | |
| can you post all of your code. I need to see all of it. the include will go in your app's main .cpp file. you're writing this in c++/cli. you can't use e as a variable, it's used by the form already. rename it. it doesn't like the functions listed there. let's see all the code. | |
|
Last edited on
|
|
| rodsRisk (14) | |||
|
The rest of the code is stuff the windows forms application automatically generated for the form I constructed using the gui. The code I posted above is whats supposed to be calculated once you click a button. The following is the very top of all the code before the automatically generated windows forms code:
| |||
|
|
|||
| TheIdeasMan (1562) | |
|
Just my 1 cent worth :) If you are including cmath, then you can use M_E for the mathematical constant e, instead of defining your own variable for it. There are a bunch of math constants defined in math.h which is included into cmath. Hope all goes well. | |
|
|
|
| kbw (5371) | |||||||
| |||||||
|
Last edited on
|
|||||||
| bobdabilder (56) | |
| I realize the gui created much of that code. if you can post all of your form code (all of it) I can help sort out the errors and post it back. | |
|
|
|
| rodsRisk (14) | |
|
I just attempted to post all the code including the GUI code but I received an error that said I can't post mssgs longer that 9000 words. However, I made the "N()" function declaration correction and the "e" variable corrections. Also attempted to declare the functions at the top of all the code but to no avail. Can anyone advise me on how to make these functions work inside of of this private button click event?? | |
|
|
|
| bobdabilder (56) | |||||
just below your #pragma endregion from your gui produced code add the function here's a snippet
Then in your button click do so:
now tweak it to fit your code. | |||||
|
|
|||||
| bobdabilder (56) | |
| This is a good point to learn classes, too. | |
|
|
|
| bobdabilder (56) | |||||
|
and the class route: myFunctions.cpp
then in your form1.h or whatever its called. right after the #pragma once at the top #include "myFunctions.cpp" and finally in your button click event
| |||||
|
|
|||||
| rodsRisk (14) | |
|
I am actually currently taking my second course in c++ and we're learning about classes now so I apologize for my inexperience. When I add the functions under "#pragma endregion" am I also declaring the functions there before the definition? And when I create the separate .cpp, am I defining all my functions under the "ints" class? | |
|
|
|
| bobdabilder (56) | |
|
sorry for the confusion: separate my two examples. you don't have to define it below pragma if you make a class. if it's not clear, I'll post more code. so if you make a class: include the .cpp file below the pragma at the top and call it in the buttonclick. if you don't make a class: define the function below pragma once and call it in the button click. let me know if that helps. stick with it, you'll get it. | |
|
|
|
| rodsRisk (14) | |||
|
bob, thanks a million I got the code below to work as far as the functions go but when I run the code, the answer returns NaN (not a number). I realized the reason was that its not retrieving the user input from inside the click event so I tried moving the boolean variables and the TryParse variables to right below #pragma and received an error stating that only static variables can be defined outside a class (or something along those lines. Any ideas as to how I can get the user input variables into my functions to perform the calculations?
| |||
|
|
|||
| TheIdeasMan (1562) | ||||
#include <math.h> Including math.h is deprecated - you should use : #include <cmath> Did you see my earlier comment about using the built in constant M_E instead of defining your own constant? You will have access to it by including cmath. Why are these variable declared twice?
Perhaps they should be member variables?
Use a constructor to take your arguments and use them to set the member variables - then any class function will have access to them. Is that what you meant? | ||||
|
|
||||
| rodsRisk (14) | |
|
I'm just trying to get the code to work so I haven't edited it completely hence the variables declared twice...will remove now. However, class functions having access to set the member variables is not what I meant. My tryparse method which parses the user input and converts them into doubles to then be manipulated by my arguments in the functions declared outside of the click event is what I'm referring to. Any ideas? | |
|
|
|
| TheIdeasMan (1562) | |||
|
If the functions declared outside the click event, were member functions of a class (CCalc say)- then you could pass the values to an object via it's constructor. In this way your functions will have access to the values. So in your tryParse function you could create a CCalc object and pass the values to it via it's constructor, like this:
The CCalc constructor would have code to set member variables to be equal to the arguments. The CCalc class would also contain functions that do calculations on the member variable values. HTH | |||
|
|
|||
| TheIdeasMan (1562) | |
|
I don't anything about windows specifically, but here are some thoughts about GUI's generally: Can the type of the input be set for a text box? That is the input must be a double, say. Can you restrict input into text boxes, so that the input can only be a number? Consider having a validate function for each text box, so that validation of the input is done as soon as it is entered. That way the Button_Click function can send the valid input straight to a calculation object. HTH | |
|
|
|
| bobdabilder (56) | |||
I'm not sure of your question. here are some variables i've defined below the namespace declaration in the form.
now those are accessible from my button click so i don't have to define those in the button click. how are you getting the values from the user? or was that your question? | |||
|
|
|||
| bobdabilder (56) | |
| Ok, you're asking me how to obtain the user values so you can access them in your form1, right? | |
|
Last edited on
|
|
| rodsRisk (14) | |
|
Bob, Yes, I'm asking how to obtain the user input values to access them in my form1. TheIdeasMan suggested creating a class that will implement my functions as member functions and establishing a getter function to obtain the tryParsed user input, if I understood him correctly. So I will try to do what he has suggested. | |
|
|
|