// 1.cpp
//-------------------
#include<iostream>
namespace np {
int i = 0;
}
//-------------------
// 2.cpp
//------------------------------------------------------------
#include<iostream>
int main()
{
std::cout << "hello" << std::endl;
np::i = 3;// How to get np::i in file 1.cpp???
}
//-------------------------------------------------------------
Instead include header files, in this case the header which has the namespace in it. I like to use *.hpp for header files - it means a header with c++ code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// np.hpp
//-------------------
#include<iostream>namespace np {
int i = 0;
}
//-------------------
// 2.cpp
//------------------------------------------------------------
#include<iostream>
#include "np.hpp"
int main()
{
std::cout << "hello\n"; << std::endl;
np::i = 3; // now the reference to the namespace works fine :+)
}
Actually, the above code may not be exactly what you want. If you end up including np.hpp in multiple source files that get linked together, you will end up with multiple definitions of np::i. You need to do the following instead.
Also, instead of getting rid of the << std::endl;, it would be better to get rid of the \n after "hello". The std::endl not only adds a new line but also flushes the output buffer
Also, instead of getting rid of the << std::endl;, it would be better to get rid of the \n after "hello". The std::endl not only adds a new line but also flushes the output buffer
Hi doug4,
I have always looked forward to what you have had to say, but could I query this one?
Because std::endl flushes the buffer, that is a reason not to use it, because it is slow. Also, doesn't the buffer flush itself automatically when required?
One can use std::cerr which forces a flush each time, if one is worried about multiple access from different threads.
I guess there are various reasons why one would or would not want to force flushing.
Alternatively, you may declare the variable inlineif you want to allow multiple definitions without linker errors.
Unless something was added in c++14 (c++11 is the latest that I have available to me), it doesn't look like variables can be inlined. That only works with functions.
Also, if the variable were inlined, then it would have to be sort of a static variable compiled into each compilation unit separately, so there would be versions of "np::i" floating around, each potentially with a different value.
@TheIdeasMan
For a beginner, the performance hit from a stream flush is trivial. I have had programs where output did not get flushed before a the program crashed, and I was misled as to what actually got written. I just think it's generally a good idea to use std::endl (that's why it was added to the standard) unless performance issues dictate otherwise.
Maybe instead of "it would be better to" in my original post I should have said "I would instead". I think it is better, but that may more of an opinion than an objective statement.
And I would never use std::cerr for standard program output. That should be used for exceptional output only (well, at least in my opinion ;) )