Functions in headers

Sep 7, 2012 at 8:40am
I'm learning C++, and I'm working on a project. In this project, I need to call functions inside functions to allow navigation on a square grid. I have my functions defined above my int main(), then a short user input that calls the first function to get it started. However, when compiling, it won't let me compile a function that calls a function that hasn't been defined yet. I was wondering how I could fix this, and if it was to put all my functions inside a header, and then include the header in the main file that calls the first function.
Sep 7, 2012 at 8:58am
Headers should only contain function prototypes:
MyHeader.h
1
2
3
4
#pragma once //so multiple includes of the same header will be ignored

int myFunction1(); //declares myFunction1, but leaves it undefined
int myFunction2(); //dito 


Then define them in a sepereate source file (or in multiple separate source files if you feel like it)

MySource.cpp
1
2
3
4
5
6
7
8
9
10
#include "MyHeader.h"
int myFunction1() {
    if(std::rand()%2) return 1;
    return myFunction2();
}

int myFunction2() {
    if(std::rand()%2) return 2;
    return myFunction1();
}


Then a mainfile using the header:
main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "MyHeader.h"

int main()
{
     std::srand(std::time(0));
     std::cout << myFunction1();
     return 0;
}

Then it depends on the compiler you're using, but if you're compiling from the command line it will look something like this (example with gcc, other compilers will obviously look a bit different, but with mostly the same process).

1
2
3
gcc -c MySource.cpp //compile MySource to MySource.o
gcc -c main.cpp //compile main to main.o
gcc MySource.o main.o -o test //link MySource.o and main.o to test 

In and IDE, this will be done automatically just by adding the source file to the project and then hitting compile. Is that clear?
Last edited on Sep 7, 2012 at 9:00am
Sep 7, 2012 at 11:49am
Place the definition of the called internally function before the calling function. Or you can only declare the called function before the calling function and set its definition later. For example


1
2
3
4
5
6
7
8
9
10
11
int f1( int );

int f2( int  x )
{
   return ( f1( x ) );
}

int f1( int x )
{
   return ( x );
}
Last edited on Sep 7, 2012 at 11:49am
Sep 9, 2012 at 4:28pm
i do not follow some of the c++ rules but you could just place all your functions
in the header without the cpp file. it worked fine with me.
Sep 9, 2012 at 4:31pm
@CLman94
If you define the functions in header files you have to make them inline functions.
Sep 14, 2012 at 12:35am
<Peter87>
I really don't know what inline functions are and according to my last reply i do not use them,but that works too.
Sep 14, 2012 at 9:32am
@CLman94
Are you sure it works if you include the header in more than one .cpp file?
Sep 14, 2012 at 11:24pm
<Peter87>
Yes it does work.
Used it all the time.
I could be wrong.
Sep 16, 2012 at 4:59am
@CLman94, what Peter87 is saying is this.

CustomHeader.h
1
2
3
void IsThisDefinedMoreThanOnce(){
   //...
}



scenario 1) Included only in main (OK) does not break ODR rule http://en.wikipedia.org/wiki/One_Definition_Rule

main
#include "CustomHeader.h"

scenario 2) Included in main and any other cpp in the project.

main
#include "CustomHeader.h"

SomeSourceFile.cpp
#include "CustomHeader.h"

Breaks ODR rule, does not work, not without inlining the functions.
Topic archived. No new replies allowed.