Learning c++, using headerfiles and forward declaration of functions.
File 1: ReadNum.cpp
#include<iostream> using namespace std; int ReadNumber() { int x; cout << "Enter a number: "; cin >> x; return x; }
File 2: WriteAnswer.cpp
#include<iostream> using namespace std; void WriteAnswer (int ans) { cout << "The answer is: " << ans << endl; }
File 3: Add.cpp
#include<iostream> using namespace std; int Add (int x, int y) { return x+y; }
Header
File 4: Task1.h
#ifndef TASK1_H #define TASK1_H int ReadNumber (); void WriteAnswer (int); int Add (int, int); #endif
File 5: main.cpp
#include<iostream> #include"Task1.h" using namespace std; int main() { int x = ReadNumber(); int y = ReadNumber(); WriteAnswer(Add(x,y)); cin.get(); // halting the prompt window cin.ignore(); // halting the prompt window return 0; }
All files are in same folder. Also the following code will compile and display correctly.
#include<iostream> #include"WriteAnswer.cpp" #include"ReadNumber.cpp" #include"Add.cpp" using namespace std; int main() { int x = ReadNumber(); int y = ReadNumber(); WriteAnswer(Add(x,y)); cin.get(); // halt prompt window cin.ignore(); return 0; }
Does anyone know why the .h file wont forward declare the functions?
Is it maybe because the files are not included in a project (i.e. not using CodeBlocks or Visual type IDE)