Now I must explain that I am a noob.

I would like to write my own header file, and use the functions from it in a program. When I get Dev-C++ to compile, it says that it is an invalid function, and I need to put that before the main. I think it may be because I've written the header wrong.

Could someone tell me how to write the header file, and then use the functions in my main source file.

Thanks

Mark

Recommended Answers

All 4 Replies

Like this:

FUNCTION.H:

// Header guards, to ensure this file gets included only once:
#ifndef FUNCTION_H_
#define FUNCTION_H_

// Function declaration
void myFunction(void);

#endif

FUNCTION.CPP:

// include the declaration
#include "function.h"
#include <iostream>

// Function definition
void myFunction() {
     std::cout << "myFunction called!";     
}

MAIN.CPP:

// include the declaration
#include "function.h"

int main () {
    myFunction(); // call the function
    return 0;
}

Thanks! Works now.

Well I say works...
It just loads, puts green background - how I want - but then multicoloured lines flash round the edge of the screen - not what I want. I'll have another look at the code later...

Thanks! Works now.

Well I say works...
It just loads, puts green background - how I want - but then multicoloured lines flash round the edge of the screen - not what I want. I'll have another look at the code later...

Whatever the problem is, it's not nick's code, which should work whether one understands the code or not. Multi-cored lines flashing? nick's example is a console application. No lines, Certainly not flashing. A console screen should pop up and "myFunction called" should display. You are in Dev C++, so I believe the console screen DOES NOT stay open. So add this line right before "return 0;"

cin.get ();

Now nick's sample program should definitely stay open and you should see "myFunction called". Press the "Enter" key to end the program.

If this does not work, there's a decent chance that you don't have your Dev C++ project set to be a simple console project or you aren't compiling it or something. Please report back with the results.

[EDIT]
Or is the code that you're going to look at your original code? When you are describing what happens, are you referring to nick's code or some other code?
[/EDIT]

No, no it's my code, written in C++ with Allegro.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.