use external varaiable

hello everyone,
I have defined external variable in hpp file (extern double average_cr=1;)
and I have change this variable in cpp file. I would like to call this variable in another function but since the multiple function include this hpp file I have gotten error for multiple definition of "average_cr"
I will become thankful, if you help me that how I can solve this problem
thanks
best regards

closed account (E0p9LyTq)
You have to actually define the the variable, not extern, in one of your .cpp files. Usually as a global:

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "second.h"

double PI = 3.14159;

int main()
{
   std::cout << "PI in main: " << PI << "\n";

   aFunction();
}


second.h:
1
2
3
4
5
6
7
8
#ifndef __SECOND_H__
#define __SECOND_H__

void aFunction();

extern double PI;

#endif 


second.cpp:
1
2
3
4
5
6
7
#include <iostream>
#include "second.h"

void aFunction()
{
   std::cout << "PI in aFunction: " << PI << "\n";
}


PI in main: 3.14159
PI in aFunction: 3.14159
Dear FurryGuy,
thank you so much for your help. it works
regards
Topic archived. No new replies allowed.