1

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)

1 Answers1

0

First, by #including the .cpp files you are making their contents visible to the compiler. Don't do that - include the header files.

Second, when you say "why the .h file won't forward declare the functions" it does declare the functions, but if you don't add the source files with the definitions to your build (project, makefile etc) you will get a linker error.

doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • Thanks. So when you say I have to add source files to the build,...I do not use an IDE environment. I use Notepad ++ and a plugin: NPPExec to call the compiler (wcc i think). I'm assuming then that using NPP is not suitable for multifile C++ programs. Am i right ? – user3115315 Jul 22 '14 at 13:58
  • If I add the following to the .h file: #include"WriteAnswer.cpp" and #include"Add.cpp" and #include"ReadNumber.cpp" . Is this an ok method ? It will compile at least... (sorry, such a newb) – user3115315 Jul 22 '14 at 14:13
  • Don't #include the cpp files. You need to tell the compiler about all the cpp files: some clues here... http://stackoverflow.com/questions/2506400/how-to-compile-and-run-c-files-from-within-notepad-using-nppexec-plugin – doctorlove Jul 22 '14 at 15:50
  • This http://daleswanson.blogspot.co.uk/2012/07/how-to-compile-c-code-in-notepad-with.html suggests F6 will give you the actual command - amend the g++ line to get it use all your cpp files. Something like `g++ *.cpp -o output` See http://stackoverflow.com/questions/3202136/using-g-to-compile-multiple-cpp-and-h-files – doctorlove Jul 22 '14 at 15:55
  • Thank you so much. Works great. Just added the source .cpp's to the g++ line – user3115315 Jul 22 '14 at 17:39