I have a number of functions I would like to reuse in multiple pieces of code.

I have put these into a single header file that looks something like the following:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

//function prototypes
extern float sum(vector<float> &v);

//calculate sum of a vector
float sum(vector<float> &v)
{
int num=v.size();
 float sum=0.;
 for(int ss=0;ss<num;ss++)
   {
     sum+=v[ss];
   }
 return sum;
}

#endif

I have included this in more than one file using a line like

#include "functions.h"

When I do this, I get multiple definition errors. I think I have done everything correctly with extern and ifndef, can anybody explain what I am missing?

You can't have function definitions in headers if they're not inline, the correct way of doing this is to put the function declaration in a header file that you include and the function definition in a .cpp file in your project.

Making my functions inline did the trick!

I think you're taking away the wrong lesson, here. While making the functions inline will work, it isn't the best practice, as inlining larger functions is very inefficient in terms of space - especially if you are calling them in several places. The real solution, in the long term, is to put only function prototypes in the header, and place the functions themselves in a separate file:

functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

//function prototypes
extern float sum(vector<float> &v);

#endif

functions.cpp

#include "functions.h"

//calculate sum of a vector
float sum(vector<float> &v)
{
int num=v.size();
 float sum=0.;
 for(int ss=0;ss<num;ss++)
   {
     sum+=v[ss];
   }
 return sum;
}

The separate file will need to be compiled separately and linked into the main program, though with most modern IDEs this is done automagically by the project feature.

This question has already been answered. Start a new discussion instead.