I have been trying a few things out with 'extern' keyword. I wrote this basic function and I am not sure why my print function is not working. Kindly help me in understanding it.
test1.h #pragma once #include<iostream> using namespace std; extern int a; extern void print(); test1.cpp #include "test1.h" extern int a = 745; extern void print() { cout << "hi "<< a <<endl; } test2.cpp #include"test1.h" extern int a; extern void print(); int b = ++a; int main() { cout << "hello a is " << b << endl; void print(); return 0; } Actual output : hello a is 746 Expected output: hello a is 746 hi 746
extern– you would observe the same missing output if all the code were in the same file. Read about how to call functions in your favourite C++ book.externby default in C++.