![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0
![]() |
I'm happy to post my code when I get home from uni.
![]()
__________________
Don't wound what you can't kill |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|