Im not sure if this is how the process would work, but if i am to say create a header file named myheader and in that header wrote

#define functionName
{
code...
}

and then wrote a .cpp file and in the source code I would #include <myheader> and I called functionName(); somewhere in the source, i would not have to write the code for that function in my source file because it is included in my header correct?

This would also allow for compiling if I called the function before I wrote one if I were to regularly just write the function code in my source?

Sorry if the question is unclear, just ask if your confused =D

thanks

Dani AI

Generated

Good question from and useful clarification from . Short answer: #define is the preprocessor doing text substitution, not a proper way to create a normal function. Headers are normally used to expose declarations (function prototypes, class declarations, typedefs, constants, inline functions and templates), while the actual function bodies usually live in a single .cpp so the linker sees exactly one definition.

Common, safe rules to follow:

  • Put only declarations in ordinary headers. Use include guards or #pragma once so repeated includes don't produce errors.
  • Put non-inline function definitions in one .cpp file and compile/link it into the program.
  • Put templates, inline or constexpr functions in headers (these are allowed to be defined in a header because the language permits multiple identical definitions).

Example alternatives (not shown earlier in the thread):

#define SQR(x) ((x) * (x))        /* macro: simple but risky with side-effects */
inline int sqr(int x) { return x * x; }   /* safer, type-checked alternative */

Why macros are risky: they perform no type checking, do not respect scope, and arguments can be evaluated multiple times (causing unexpected side effects). For most cases a small inline or constexpr function is clearer, safer, and debuggable.

Troubleshooting hints tied to the thread: if the compiler complains about calling an undeclared function, a declaration (prototype) is missing before the call; if the linker reports "undefined reference" the function body wasn't linked in; if it reports "multiple definition" a non-inline definition appears in headers included by more than one .cpp. Following the declaration-in-header / definition-in-one-cpp pattern avoids these problems.

Recommended Answers

All 3 Replies

This is absolutely awful. Do not do what you are talking about doing.

Tell us what your problem is, what you really are trying to achieve, instead of asking about one particular mechanical option, so that a better solution can be recommended.

I'm just curious as to how the system works, and i am obviously still a beginner in programming. I'm trying to figure out how to write headers and what exactly they do. I know that they carry pre defined functions correct? So how do i make my own?

Header files typically contain declarations of functions, not their definitions. Typically, the implementations are placed in a similarly named cpp file. Your project will then have multiple cpp files.

Also, #define is not how you declare or define functions; I don't know what you're thinking.

For example, I might want to have a function fooz available by a header file "foo.h". So in foo.h I'll have

#ifndef guard_foo_h_
#define guard_foo_h_

int fooz();

#endif /* guard_foo_h_ */

And in foo.cpp

int fooz() {
    return 4;
}

Then, some other file, like bar.cpp, might #include "foo.h" so that the compiler sees the declaration of fooz. Or the writer of bar.cpp could just manually write

int foo();

to inform the compiler about the declaration.

Then, after you compile foo.cpp and bar.cpp and link them together, you'll get a working program where a function written in bar.cpp calls a function written in foo.cpp.

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.