C++/c++
Expert: Eddie - 11/14/2008
QuestionSir, 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.
AnswerHello 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