| | |
[need help] Initializing Objects in Control Structures
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 2
Reputation:
Solved Threads: 0
I'm having trouble initializing Objects while they're inside if else statements. I'm using Visual C++ 2008.
for instance:
it would me this error:
i know that this means, i just don't know if there's a better way to do this.
i'm required to take account information and initialize the object depending on the user's input.
i was thinking i'd use if else statements then create the object, but apparently that doesn't work.
for instance:
C++ Syntax (Toggle Plain Text)
class Account { Account(); Account(int = idnum, double creditLimit = 500.00); Account(double initBal, int idnum, double creditLimit = 100.00); public: somefunction(); }; if (a > 0 && b > 0) { Account customer(a,b); } else { Account customer; } customer.somefunction();
it would me this error:
•
•
•
•
error C2065: 'customer' : undeclared identifier
error C2228: left of '.somefunction' must have class/struct/union
i'm required to take account information and initialize the object depending on the user's input.
i was thinking i'd use if else statements then create the object, but apparently that doesn't work.
No need to have this if else , use customer(a,b) , in the constructor check if a>0 , b>0 if no set the default value.
You declare local variables in if-else alternatives. Of course, they were discarded outside if-else statement:
C++ Syntax (Toggle Plain Text)
if (a > 0 && b > 0) { // New block started Account customer(a,b); // Local variable defined (on the stack) } else { // Prev. bock is ended. Another block started. Account customer; // Local variable defined } // All customers dead... RIP... // Alas, no customers here...
Hi maybe the following suits your problem:
Which gives for examples following outputs depending on what u enter:
c++ Syntax (Toggle Plain Text)
#include <iostream> class Account { public: Account() : initBal(0),idnum(0),creditLimit(0.0) {} Account(int id): idnum(id) { initBal=100; creditLimit=500.0; } Account(int balance,int id): initBal(balance),idnum(id) { creditLimit=100.0; } public: void somefunction() { std::cout<<"Initial balance is: "<<initBal<<std::endl; std::cout<<"id is: "<<idnum<<std::endl; std::cout<<"credit Limit is: "<<creditLimit<<std::endl; } Account& operator=(const Account& other) { if (&other != this) { initBal = other.initBal; idnum = other.idnum; creditLimit = other.creditLimit; } return *this; } private: int initBal; int idnum; double creditLimit; }; int main() { Account customer; int a,b; std::cout<<"please enter a value for a: "<<std::endl; std::cin>>a; std::cout<<"please enter a value for b: "<<std::endl; std::cin>>b; if (a > 0 && b == 0) { Account alocalcustomer(a); customer = alocalcustomer; } else if (a > 0 && b > 0) { Account anotherlocalcustomer(a,b); customer = anotherlocalcustomer; } customer.somefunction(); return 0; }
Which gives for examples following outputs depending on what u enter:
c++ Syntax (Toggle Plain Text)
//---------1st case : please enter a value for a: 0 please enter a value for b: -1 Initial balance is: 0 id is: 0 credit Limit is: 0 //---------2nd case : please enter a value for a: 1 please enter a value for b: 0 Initial balance is: 100 id is: 1 credit Limit is: 500 //---------3rd case : please enter a value for a: 2 please enter a value for b: 320 Initial balance is: 2 id is: 320 credit Limit is: 100
It's a good idea but not so good implementation. No need to define assignment operator: it's exactly default assignment. Keep it simpler:
C++ Syntax (Toggle Plain Text)
Account customer; ... if (a > 0 && b > 0) customer = Account(a,b); customer.somefunction();
For sure but it seemed to me, that he wanted to use the "some.function" outside the local scopes of the if statements while initializing objects there.
So I tried to give him such a solution.
So I tried to give him such a solution.
•
•
•
•
It's a good idea but not so good implementation. No need to define assignment operator: it's exactly default assignment. Keep it simpler:
C++ Syntax (Toggle Plain Text)
Account customer; ... if (a > 0 && b > 0) customer = Account(a,b); customer.somefunction();
•
•
Join Date: Apr 2008
Posts: 60
Reputation:
Solved Threads: 1
Your problem is that the account objects are going out of scope when the if statement ends and their destructor's are being called. Initialized it outside of the if else statement and then just modify it as needed in the if statements. If you don't know what I mean by going out of scope just do a quick Google search. This is something every programmer should know about.
![]() |
Other Threads in the C++ Forum
- Previous Thread: closing a function
- Next Thread: Sprite/Bitmap Graphics
| Thread Tools | Search this Thread |
api array beginner bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion count database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






