This is a discussion on Switch statement within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by vart and why exactly do you need declare reslut as pointer to double and allocate it dynamically? ...
I noticed a rather unfortunate choice of variable names:
The C++ Standard states that "each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercaseCode:double _Num1; double _Num2; double _Num3;
letter is reserved to the implementation for any use". As such, you should use a different variable naming convention. It seems to me that something as straightforward as num1, num2 and num3 will do.
It also seems that these three variables were not really used at all. Likewise, it looks like much of your class actually functions as a namespace with the member variables left largely unused except for result. After all, although you use Num1 and Num2 in various member functions, you have parameters of the same name, thus those parameters are used and the member variable names are hidden.
I have went over my code and realised that it was a bit of a mess, thanks to the people on this bored for helping me realise this, so I have reformatted(I think that's the correct word) my code and come across a few errors, I have moved....
....to the constructor and removed the other declarations from the program, but I don't see why i get these errors:Code:double num1; double num2; double num3; double Num1; double Num2; double Num3;
I thought that whenever an object is made that the constructor is called. So why are my variables not declared?Code:main.cpp 28 error: 'num1' undeclared(first use this function)
Am I missing something here?
Last edited by beene; 07-01-2007 at 03:35 AM.
I believe variables declared inside a constructor are local to that constructor, just like a regular function. You need variables that have class-wide scope to access them anywhere inside the class.
Right, is there another way to tidy up my code without making the variables global?
Yes: declare local and member variables as needed and keep them and their scopes straight.
All the buzzt!
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law