S
subramanian100in
Consider the following program x.cpp:
#include <cstdlib>
#include <iostream>
using namespace std;
namespace Sample
{
extern int sampleInt = 100;
}
using namespace Sample;
extern int globalInt = 200;
int main()
{
cout << sampleInt << endl;
cout << globalInt << endl;
return EXIT_SUCCESS;
}
When I compile this program using g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
I get the following compiler warnings:
x.cpp:8: warning: `sampleInt' initialized and declared `extern'
x.cpp:13: warning: `globalInt' initialized and declared `extern'
I read in "C++ Primer 4th edition" by Stanley Lippman (page 53) that
"An extern declaration may include an initializer only if it appears
outside a function and an extern that is initialized is treated as a
definition".
If this is so, why does the g++3.4.3 compiler generate warning
messages ? Is my program wrong ?
Kindly explain.
Thanks
V.Subramanian
#include <cstdlib>
#include <iostream>
using namespace std;
namespace Sample
{
extern int sampleInt = 100;
}
using namespace Sample;
extern int globalInt = 200;
int main()
{
cout << sampleInt << endl;
cout << globalInt << endl;
return EXIT_SUCCESS;
}
When I compile this program using g++3.4.3 as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
I get the following compiler warnings:
x.cpp:8: warning: `sampleInt' initialized and declared `extern'
x.cpp:13: warning: `globalInt' initialized and declared `extern'
I read in "C++ Primer 4th edition" by Stanley Lippman (page 53) that
"An extern declaration may include an initializer only if it appears
outside a function and an extern that is initialized is treated as a
definition".
If this is so, why does the g++3.4.3 compiler generate warning
messages ? Is my program wrong ?
Kindly explain.
Thanks
V.Subramanian