problems with source/header files

Okay, so I have a source file, rangedRandom.cpp, that holds two functions for getting random numbers within supplied ranges.

I have two other source files that use the rangedRandom functions. I try to compile, but one of them complains that rangedRandom functions haven't been defined. Do I need to make rangedRandom a .h file and then #include it in both files? I tried that and got multiple declaration errors, even though I used header guards..

(Please know that this is the first time I'm trying to work with header/source files, and am very confused right now)
See: http://www.cplusplus.com/forum/articles/10627/

It seems you have it basically correct, but I'd have to see the code to be sure.
From what I'm reading, it looks like whenever you make a header/source file, all functions inside should be part of some class. That might be my problem then - I had written the rangedRandom functions without any class.. is there a way for me to avoid writing a class for it? As in, simply writing:


randRange.cpp
1
2
3
4
int    random::randomInRange(int a, int b)             // random int between [a,b]
{
    randInt = rand()%(b-a)+a;
}


main.cpp
 
cout << randomInRange(0,20);


Functions don't need to be in a class. Just put them in the header file and it should be fine ;)
:( Okay, I tried putting it in a header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef RANGEDRANDOM_H_INCLUDED
#define RANGEDRANDOM_H_INCLUDED

using namespace std;

// HELPER FUNCTIONS -----------------------------
int    randomInRange(int a, int b)             // random int between [a,b]
{
    return rand()%(b-a)+a;
}

double randomInRange(double a, double b)    // random double between [a,b]
{
    double randNum = static_cast<double>(rand())/static_cast<double>(RAND_MAX);
    return randNum*(b-a)+a;
}
// ----------------------------------------------

#endif // RANGEDRANDOM_H_INCLUDED 


I'm still getting multiple definition errors :/ Shouldn't the include guard be taking care of this?
You put the prototypes in the header.

You put the implementation (function body) in the cpp file.

The function body can only go in the header if the function is a template or is inlined.

Your header:
1
2
3
4
5
6
7
8
9
#ifndef RANGEDRANDOM_H_INCLUDED
#define RANGEDRANDOM_H_INCLUDED

// HELPER FUNCTIONS -----------------------------
int    randomInRange(int a, int b);             // random int between [a,b]
double randomInRange(double a, double b);    // random double between [a,b]
// ----------------------------------------------

#endif // RANGEDRANDOM_H_INCLUDED  


Your cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include "rangedrandom.h"

using namespace std;

int    randomInRange(int a, int b)
{
    return rand()%(b-a)+a;
}

double randomInRange(double a, double b)
{
    double randNum = static_cast<double>(rand())/static_cast<double>(RAND_MAX);
    return randNum*(b-a)+a;
}
Oh! I should have done more than skimmed that article. Changing the #include "rangedrandom.h" into foward declarations of it worked. Thanks for all the help, this was a nightmare.

Edit:

Oh, alright! Thank you, disch.
Last edited on
Topic archived. No new replies allowed.