0

I would like to use external variables as parameters in several c++ files. How do I use a namespace with external vars? Without the namespace everything works.

crack_detection_opencv.cpp:-1: In function ‘void ProcessArgs(int, const char**)’:
fillSobelSearchSize = std::stoi(optarg);
reference to ‘fillSobelSearchSize’ is ambiguous

Shortened code:

void ProcessArgs(int argc, const char** argv_orig)
{
    fillSobelSearchSize = std::stoi(optarg);
}
#pragma once

namespace cd
{
    float contrastAlpha = 5.8f; //unused
    float contrastBeta = -2.8f; //unused
    float contrastAlpha2 = 5.6f; //unused
    float contrastBeta2 = -2.8f; //unused
    int fillSobelSearchSize = 20;
}
using namespace cd;

extern float contrastAlpha;// = 5.8f;
extern float contrastBeta;// = -2.8f;
extern float contrastAlpha2;// = 5.6f;
extern float contrastBeta2;// = -2.8f;
extern int fillSobelSearchSize;

I tried to use external global variables as parameters in several functions and wrap them in a namespace. Without the namespace it works fine, but there are compilation erros if I use a namespace.

stupidstudent
  • 678
  • 4
  • 13
  • you expect `using namespace cd;` to somehow effect the following declarations? – 463035818_is_not_an_ai Feb 03 '23 at 13:18
  • 2
    You've got a global `fillSobelSearchSize` (see `extern int fillSobelSearchSize;`) and one in a namespace (`cd`). The compiler is telling you this. – Mike Vine Feb 03 '23 at 13:18
  • 1
    It's very simple. ***Declare*** the variables as `extern` in a header file. ***Define*** the variables in one of the cpp files, and their values. – Sam Varshavchik Feb 03 '23 at 13:19
  • Oh so I switched the way it should be done. But I want to give define some start values in the header file for each variable, so I can use it as a library. This won't be possible with external variables, right? Or can I just set them to the value after using extern? – stupidstudent Feb 03 '23 at 13:53

0 Answers0