expected unqualified-id

Hi there
I am just starting out in programming and using Both the internet and books to learn C++
this code is direct from book. I am getting the error

expected unqualified-id before '{' token

on lines 32,38,44 these i have marked /////////this is first error
I am using code::blocks mingGW 64 bit
I have searched the net and your forum and only found ;after the main () I don't have this
any suggestions please

//Global reach
//Demonstrates global variables

#include <iostream>

using namespace std;

int glob = 10; // global variable

void access_global();
void hide_global();
void change_global();

int main()

{
cout << "\t*****GLOBAL REACH*****\n\n";
cout << "In main() glob is: " << glob << "\n\n";
access_global();

hide_global();
cout << "In main() glob is : " << glob << "\n\n";

change_global();
cout << "In main() glob is : " << glob << "\n\n";

return 0;

}

void access_global();

{///////// this is the first error
cout << "In access_global() glob is: " << glob "\n\n";
}
void hide_global();

{///////// this is the second error
int glob = 0;
cout << "In hide_global() glob is: " << glob "\n\n";

}
void change_global();

{///////// this is the third error
glob == -10 // change global variable glob
cout << "In change_global() glob is: " << glob "\n\n";
}



just remove the semicolons after access_global), hide_global(), and change_global(). With semicolon it's a declaration and the compiler doesn't expect a function body
Hmmm I tried that and it now tells me that ';' expected before string constant

I am going to try it on a diffrent compiler.
That is when I sift through all the net has to offer

I am grasping this slowly

Last edited on
Fixes.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//Global reach
//Demonstrates global variables

#include <iostream>
using namespace std;

int glob = 10; // global variable
// Recommend capitalizing when you make something global. That defines it being Global Scope.
// Just a naming convention. Not a Rule.

void access_global();
void hide_global();
void change_global();

int main()
{
  cout << "\t*****GLOBAL REACH*****\n\n";
  cout << "In main() glob is: " << glob << "\n\n";
  access_global();

  hide_global();
  cout << "In main() glob is : " << glob << "\n\n";

  change_global();
  cout << "In main() glob is : " << glob << "\n\n";

  return 0;
}

// FIXES MOSTLY BELOW THIS LINE
void access_global() // Bad semicolons after function names
{
  cout << "In access_global() glob is: " << glob << "\n\n"; // You forgot a "<<" after "glob"
}

void hide_global()
{
  // Redefined a new glob. BAD.
  // Guessing this was on purpose
  int glob = 0;
  cout << "In hide_global() glob is: " << glob << "\n\n"; // You forgot a "<<" after "glob"
}

void change_global()
{
  glob = -10; // This was a check if glob is equal to -10. Not assignment. Removed one "=".
  // Forgot a semicolon in previous line.
  cout << "In change_global() glob is: " << glob << "\n\n"; // You forgot a "<<" after "glob"
}
hey wolfgang thanks heaps for that

as i said that code was directly from book , not a great deal of info on global variable in the chapter
Going to have to get some more books in this early stage

thanks once again


Im guessing I got a long way to go with this language lol
Topic archived. No new replies allowed.