To make n1::xaccessible from main.cpp, you probably want to create and enable sample.h:
#ifndef SAMPLE_H
#define SAMPLE_H
namespace n1
{
extern int x;
}
#endif
#include "sample.h"
namespace n1
{
int x = 42;
}
#include <iostream>
#include "sample.h"
int main()
{
std::cout << "n1::x is " << n1::x;
}
If you prefer not to create a header file, you can also do this in your main.cpp:
#include <iostream>
namespace n1
{
extern int x;
}
int main()
{
std::cout << "n1::x is " << n1::x;
}
source
share