PDA

View Full Version : What is wrong with my code?




Soulstorm
Mar 27, 2005, 04:24 PM
[quote]#include <iostream>
using namespace std;

void func1();
void func2();

int count; // this is a global variable

int main()
{
int i; // this is a local variable

for(i=0; i<10; i++) {
count = i * 2;
func1();
}

return 0;
}

void func1()
{
cout << "count: " << count; // access global count
cout << '\n'; // output a newline
func2();
}

void func2()
{
int count; // this is a local variable

for(count=0; count<3; count++) cout << '.';
}



Soulstorm
Mar 27, 2005, 04:32 PM
When I write this code, all I get is errors concerning the declaration of "count" variable. I get those errors from both DevC++ and XCode...

I saw this code in the book named "C++ from the ground up, third edition by herbert schildt".

What is wrong with this code?

Mitthrawnuruodo
Mar 27, 2005, 04:42 PM
Edit: Forget it... I'll look better... :o

How about wrapping a code tag around your code... lets see the indents... ;)

Edit2: It's been a while, and I don't think this is the error, but is it wise to have a local and a global variable with the same name...?

Edit3: On second thought, change the local variables name, and see if it will compile and run...

robbieduncan
Mar 27, 2005, 04:52 PM
It's something to do with count being used already in the std namespace. If you replace all instances of count with count1 then g++ can compile it fine and it runs as expected.

Edit: note that using the same name for a local and global variable is fine. This looks like code designed to show what happens when you do that and how the scope rules work.

Soulstorm
Mar 27, 2005, 04:55 PM
It's something to do with count being used already in the std namespace. If you replace all instances of count with count1 then g++ can compile it fine and it runs as expected.

Edit: note that using the same name for a local and global variable is fine. This looks like code designed to show what happens when you do that and how the scope rules work.

indeed! thanks, man!

One question though. Was this "count" thing added into the c++ after year 2003? how can it be that the book writes the code using "count", since it is already used by the c++ language?

robbieduncan
Mar 27, 2005, 05:01 PM
indeed! thanks, man!

One question though. Was this "count" thing added into the c++ after year 2003? how can it be that the book writes the code using "count", since it is already used by the c++ language?

To be honest I've no idea. I don't really use C++. Neither does your code apart from using C++ style output. I am just used to reading compiler errors! It is possible that the author of the book was using an old toolchain so did not see the errors.

It appears to be a clash with something in the STL which is not nearly as standard as the S in the acronym might have you believe! I'd move on and not worry about it. The code still teaches you the lesson it's meant to and you are unlikely to encounter this in real life.

Is DevC++ based on gcc (XCode is)? The clash may only occur in gcc implementations.