C++/c++

Advertisement


Question
Sir, I am using Turbo c++ 4.5 software and self learning c++.When I compile the following program :

#include <iostream.h>
int  main ()
{ namespace Block1
{ int x  = 44;
cout << x << endl;          // prints 44
}
namespace Block2
{ int x = 66;
cout << x << endl;          // prints 66
}
cout << Block1::x << endl;          // prints 44
cout << Block2::x << endl;          // prints 66
}
the compiler shows 4 total errors. The first error is shown as:Undefined symbol 'namespace' in function main.The second error is : statement missing in function main. The third error is :Type qualified block2 must be struck or class name in function main.The fourth error is:Statement missing in function main.
Sir I think that the compiler I am using does not recognize the 'namespace' or 'block' concepts.I am struck here. I am using this compiler as this I have got free. Kindly help me.Sir, incidentally I may tell you that I am 53 year old man.

Answer
Hello Susjoy, thank you for the question.

In C++, you cannot define namespaces within the main function. They should be defined in an area above main like such:

#include <iostream.h>

namespace Block1
{
int x  = 44;
cout << x << endl;          // prints 44
}

namespace Block2
{
int x = 66;
cout << x << endl;          // prints 66
}

int  main ()
{
cout << Block1::x << endl;          // prints 44
cout << Block2::x << endl;          // prints 66
}

I'm also not sure if you can place cout statements in a namespace declaration. That might lead to subsequent compile errors.

Also, remember that if you wish to use cout without std:: in front of it, you must add using namespace std; to the top of the file.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.