You may not use assignment statements outside functions. You may only declare or/and define objects . So the valid code will be
namespace
{
int a = 5;
int b = 10;
}
main.cpp
#include <iostream>
#include "xx.h"
int main()
{
std::cout << "values" << a << b << std::endl;
}
Take into account that objects declared in unnamed namespaces have internal linkage. It means that if this header is included in more than one module then in each module it declares a separate namespace.
Consder the following example
unnamed.h
namespace
{
int a = 5;
int b = 10;
}
void f();
second.cpp
#include <iostream>
#include "unnamed.h"
void f()
{
a = 10; b = 5;
std::cout << "a = " << a << ", b = " << b << std::endl;
}
main.cpp #include #include "unnamed.h"
int main()
{
f();
std::cout << "a = " << a << ", b = " << b << std::endl;
return 0;
}
The output is
a = 10, b = 5
a = 5, b = 10