Global scope

Could someone please tell me why this will not compile :S

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Use a global variable.

#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 << '.';
}


This is quite odd considering it's from a book i'm studying, with a known author...
I don't see errors in the code, what does the error messages say?
there is already something in the std namespace called count
Change count to some other name like cnt to avoid this problem.



(or remove the using namespace std directive - but you will have to
put std::cout )
Last edited on
Yeah i changed the name and it solved it perfectly, thank you... its crazy how much some of you lot know lol.

Off topic, is there any rule of thumb on when to use double or single quotation marks?
single quotations are for single characters 'a' 'b' 'c', double quotations are for strings and have a hidden character, the null character. so "a" is actually 'a' and '\0'.

tl;dr 'x' is for characters
"string" is for strings (anything more than one letter)
so if you was going to write for example :
 
cout << 1 + 1 <<  \n


is it better to use double or single quotes for \n
Because you are using cout to output a string of characters, I believe that double quotes are more commonly used but single quotes can be used because \n is a character.
Last edited on
Better yet, don't ever write this into your cpp files.
using namespace std;

Think of all the other names that are now going to cause a problem. Is it so terrible to just write std::cout and std::cin and std::endl? Keeping the "std::" with the usage of those std names results in clearer code and you can have a variable called count if you'd like because there won't be a naming conflict at all.
I find it a hassle to write "std::" before cout. Another approach, and better than using namespace std;, is to use a more specific using declaration. For example, "using std::cout;" will allow you to use the unqualified "cout" and will not bring anything else from namespace std into scope.
I agree with seymore but it is mostly a matter of preference. I personally prefer using std::cout to writing std:: before each line partially because of the chance that I would slip and only put one : or some other syntax error that I could have prevented myself from having to debug later but that is just me.
Last edited on
seymore you said about not wanting to bring anything else from the namespace std into scope... what other things are there? apart from the given cout, cin, endl ^^
The Reference section on this site is comprehensive. Check it out; read through the iostream library section.
Topic archived. No new replies allowed.