cout << something that may not exist
Apr 2, 2011 at 2:32pm
I need to use an if statement to create an object of a class and then use std::cout to print the object data afterwards.
The only problem is, that if I use an if statement, the object may not exist.
Here is a sample of the code I'm using:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
if( nameInput == "dog" )
Animal an( 1, 0, 0, charInput );
else if( nameInput == "cat" )
Animal an( 0, 1, 0, charInput );
else if( nameInput == "bird" )
Animal an( 0, 0, 1, charInput );
else
{
std::cout << "\n\nERROR creating animal!\n\n";
return 0;
}
std::cout << an << '\n';
|
I have an overloaded function for the std::cout << <object>.
I'm recieving the error: 'an' : undeclared identifier; on line 16.
Is there a way passed this? A way to print something that may not actually exist, given the right circumstances.
Thanks for any help.
Apr 2, 2011 at 2:53pm
Create "an" before your if statements, maybe with a pointer, then you can call new inside the if statements and pass in the necessary params.
Apr 2, 2011 at 3:07pm
Thanks. I've done that now.
Created it before and passed params afterwards.
Topic archived. No new replies allowed.