Question

I'm learning C++ and I'm at a fairly low level still. - I'm currently looking at using header files and I have a two part question.

Part 1

As I understand them so far, header file definitions are similar to hard coded strings within VB.net? For example I can do the following #define STOCK_QUANTITY 300 and then reference my definition when using it within functions? I assume this works the same way as VB.net strings as I only need to change the value in one place should I need to make changes to the definition and my program references the number 300 on a few hundred different lines?

Part 2

Now, as I said I'm still learning so I'm still doing ye old multiplication tasks. I'm good within using functions with my main .cpp file but I'm not moving on to header files. This is my code snippet thus far.

add.h

#ifndef ADD_H
#define ADD_H

int add(int x, int y);

#endif 

main.cpp

#include "stdafx.h"
#include <iostream>
#include "add.h"

int main()
{
    using namespace std;
    cout << add(3, 4) << endl;
    return 0;
}

When trying to run this I receive 2 build errors and it will not compile. enter image description here

Apologies is these are silly questions, but I would appreciate insight, tips or even some other things I should consider. Thanks.

EDIT

Based on an answer I've changed my add.h too

#ifndef ADD_H
#define ADD_H

int add(int x, int y)
{
    return x + y;
}

#endif

As soon as I read the answer I realised I hadn't even told the function what to do :( - Gosh.

Was it helpful?

Solution

You have not added a function body for the function add

int add(int x, int y)
{
    // add stuff here
}

This can be done in either the header file, or a seperate cpp file for add. You are trying to call the function that has no code. This is known as a function prototype.

OTHER TIPS

You have only declared that the function add exists, which is why you can call it. But you don't actually define the function anywhere.

In C++ you have to differ between declaration and definition. Sometimes those are done at the same time, like when declaring a local variable. Other times you separate them, like when having a function prototype in a header file (like you do) then that's the declaration. The definition of the function is then the implementation of the function.


After the edit, you now have another problem. And that is if you include the header file in multiple source files, each of those source files (formally known as translation units) will have the function defined and you will get an error because of that.

It can be solved by making the function static or inline. Or better yet, create a new source file (like add.cpp) where you have the definition of the add function, and keep only the declaration (prototype) in the header file.

As for part 1 of your question: While #defines do as you described, there are some of disadvantages with using them (e.g. They are parsed by the pre-processor, and not by the compiler so no type checking, and also you can get funky errors if you have slightly more complicated macros. So while your statement is valid, in that it defines a global constant that you only need to modify in one place, and can use in multiple places, for that purpose it is better to have an actual constant variable in the header file: e.g.

const int STOCK_QUANTITY = 300;

Now you explicitly name it an integer, and also the compiler can perform extra checking.

See also Item 3 in Effective C++ by Scott Meyer (which is a must read in my opinion if you are serious about contineouing programming in c++, although you need allready some experience before reading that.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top