Issues Declaring and Recognizing Global Functions

I created some math functions that will be used in main () and member functions in several host classes. I thought it would be easiest to make these mathematical functions global, but I'm not sure how to do this.

Currently, I put all the functions in a Rdraws.cpp file with prototypes in Rdraws.h. Even with all #includesand externs, I get a compiler error "character not found" when I first call the function in main ().

Here is what I have:

// Rdraws.cpp
#include <cstdlib>
using namespace std;
#include <cmath>

#include "Rdraws.h"
#include "rng.h"

extern RNG rgen // this is the PRNG used in the simulation; global scope

void rmultinom( double p_trans[], int numTrials, int numTrans, int numEachTrans[] )
{ // function 1 def 
}

void rmultinom( const double p_trans[], const int numTrials, int numTrans, int numEachTrans[]) 
{ // function 2 def
}

int rbinom( int nTrials, double pLeaving )
{ // function 3 def
}

// Rdraws.h

#ifndef RDRAWS
#define RDRAWS

void rmultinom( double[], int, int, int[] );
void rmultinom( const double[], const int, int, int[] );
int rbinom( int, double );

#endif

// main.cpp
...
#include "Rdraws.h"
...

extern void rmultinom(double p_trans[], int numTrials, int numTrans, int numEachTrans[]);
extern void rmultinom(const double p_trans[], const int numTrials, int numTrans, int numEachTrans[]);
extern int rbinom( int n, double p );

RNG rgen; // global PRNG object created for simulation

int main() { ... }

I am new to programming. If there is a more reasonable way to do this, I would like to know.


Update

I am an idiot and do not understand that I have not yet included Rdraws.cpp in my compiler. As the poster noted, I also forgot the semicolon.

, .

+3
3

? , .

:

g++ -c -Wall -O2 main.cpp
g++ -c -Wall -O2 Rdraws.cpp

, ...

g++ -s main.o Rdraws.o
+2

:

  • rgen . Rdraws.cpp, .
  • , ( ) main.cpp Rdraws.cpp main.o Rdraws.o, .
+1

Looks like Brian R. Bondi has the answer. I would like to point out that you do not need these function declarations externin your main.cpp. The announcements are already made in the Rdraws.h header file that you included.

0
source

Source: https://habr.com/ru/post/1741113/


All Articles