Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 14th, 2006, 8:21 PM   #1
Ade
Hobbyist Programmer
 
Ade's Avatar
 
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0 Ade is an unknown quantity at this point
error C2065: 'unit_counter2' : undeclared identifier

Hey guys, strange error I'm getting at the moment. Visual Studio is saying that 'unit_counter2' is undeclared yet it's used in the code before the line on which it is meant to be undeclared, if you know what I mean? The variable is definatly used before that line and is definatly, therefore, declared.



This is the section of code which creates the problems which appears at the very end of my programme.

	ofstream fileout;
	string unitback;
	unitback = "unitfile.txt";
	fileout.open( unitback.c_str() );

while ( unit_counter2 >= 0 )
{		
	fileout << unit_id_st[unit_counter2];
	fileout << unit_type_st[unit_counter2];
	fileout << unit_side_st[unit_counter2];
	fileout << unit_x_st[unit_counter2];
	fileout << unit_y_st[unit_counter2];
	fileout << unit_turn_st[unit_counter2];
	fileout << unit_hp_st[unit_counter2];
	fileout << unit_status_st[unit_counter2];
	if (unit_counter2 != 0)
	{
	fileout << endl;
	}

	
	unit_counter2++;
}
  fileout.close();



Any ideas why this bit of the code creates errors?
__________________
Don't wound what you can't kill
Ade is offline   Reply With Quote
Old May 14th, 2006, 9:30 PM   #2
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
On which line you're getting the error? May be you need to show more code and also the declaration.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old May 15th, 2006, 12:20 AM   #3
Twilight
Programmer
 
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3 Twilight is on a distinguished road
And unless I missed something, it seems like it's going to infinite loop as well. Since your counter must be greater than 1, but only ever gets incremented, you may have some issues there, and that could be what VS is getting at.
Twilight is offline   Reply With Quote
Old May 15th, 2006, 6:07 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The error message has nothing to do with your bad loop. The compiler isn't a mind reader. Show your declaration and the line at which the error occurs.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 15th, 2006, 6:14 AM   #5
Ade
Hobbyist Programmer
 
Ade's Avatar
 
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0 Ade is an unknown quantity at this point
You were right about the infinate loop, oops, and I put in the declaration again the line above the error and it fixed it.

Anyone know why that created an error though as I believe the compiler reads the code from top to bottom and the variable had already been declared and used at the top of the code?
__________________
Don't wound what you can't kill
Ade is offline   Reply With Quote
Old May 15th, 2006, 6:28 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Again, you're asking us to judge code you're refusing to show, for some reason. Despite that, I'll peer into the crystal ball and guess that it was defined in a place such that it fell out of scope before you used it again.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 15th, 2006, 7:11 AM   #7
Ade
Hobbyist Programmer
 
Ade's Avatar
 
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0 Ade is an unknown quantity at this point
I'm happy to post my code when I get home from uni.
__________________
Don't wound what you can't kill
Ade is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:43 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC