Mac What is wrong with my code?

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
#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

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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?
 
Comment

Mitthrawnuruodo

Moderator emeritus
Mar 10, 2004
13,808
179
Bergen, Norway
Edit: Forget it... I'll look better... :eek:

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...
 
Comment

robbieduncan

Moderator emeritus
Jul 24, 2002
24,782
141
Harrogate
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.
 
Comment

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
robbieduncan said:
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?
 
Comment

robbieduncan

Moderator emeritus
Jul 24, 2002
24,782
141
Harrogate
Soulstorm said:
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.
 
Comment

Similar threads

  • BootLoxes
9
Replies
9
Views
2K
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.