1

i have to use my varibles in entire project. I have just defined two string varibles in a namespace and reference this header in main.cpp.

Here App.h and App.cpp:

App.h

#include <string>
#include <iostream>

using namespace std;

namespace App
{
    extern string FingerPrint;
    extern string FingerPrintID;
}

App.cpp

#include "App.h"

using namespace std;

string FingerPrint = "";
string FingerPrintID = "";

And i'm trying to use this varibles in main.cpp like this:

main.cpp

#include "App.h"
#include "Helper.h"

using namespace std;

int main()
{
    App::FingerPrint = Helper().GetFingerPrint();
    App::FingerPrintID  = Helper().GetFingerPrintID();

    cout<<"FingerPrintID: "<<App::FingerPrintID<<endl;
    cout<<"FingerPrint: "<<App::FingerPrint<<endl;

    return 0;
}

When i compile this code i get this errors:

CMakeFiles/HardwareService.dir/main.cpp.o: In function main': /home/debian/Development/clion-workspace/app/main.cpp:19: undefined reference toApp::FingerPrint' /home/debian/Development/clion-workspace/app/main.cpp:20: undefined reference to App::FingerPrintID' /home/debian/Development/clion-workspace/app/main.cpp:23: undefined reference toApp::FingerPrintID' /home/debian/Development/clion-workspace/app/main.cpp:24: undefined reference to `App::FingerPrint'

But if i dont use namespace and use this varibles without 'App::', it works. Like this:

App.h

#include <string>
#include <iostream>

using namespace std;

extern string FingerPrint;
extern string FingerPrintID;

App.cpp

#include "App.h"

using namespace std;

string FingerPrint = "";
string FingerPrintID = "";

main.cpp

#include "App.h"
#include "Helper.h"

using namespace std;

int main()
{
    FingerPrint = Helper().GetFingerPrint();
    FingerPrintID  = Helper().GetFingerPrintID();

    cout<<"FingerPrintID: "<<FingerPrintID<<endl;
    cout<<"FingerPrint: "<<FingerPrint<<endl;

    return 0;
}

No problem like this.

Can i use global varibles with namespace? If i can, how can i use?

Marijke Buurlage
  • 331
  • 5
  • 21
  • 1
    But the variables are *not* global. At least they are not *declared* as global, they are declared in the `App` namespace. You do not, however, *define* them in the `App` namespace. – Some programmer dude Nov 28 '17 at 13:24
  • 1
    Also, [`using namespace std;` is not good practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), especially [in header files](https://stackoverflow.com/questions/14575799/using-namespace-std-in-a-header-file). – Some programmer dude Nov 28 '17 at 13:25
  • https://stackoverflow.com/questions/2029272/how-to-declare-a-global-variable-that-could-be-used-in-the-entire-program did i do it wrong? – Marijke Buurlage Nov 28 '17 at 13:25
  • Global variables are outside any namespace. – Some programmer dude Nov 28 '17 at 13:26
  • 1
    Continuing from @Someprogrammerdude; this is sometimes known as the "global namespace". Your variables are however "global" in that anything can access them. You may want to consider your design though - as global variables tend to be the result of bad design. – UKMonkey Nov 28 '17 at 13:28
  • i see, can u guys link samples to learn these designs for me, i m very very beginner. – Marijke Buurlage Nov 28 '17 at 13:37
  • 1
    On a side note, good question; you tried, you did research and then asked posting everything. If only all new starters did this. – UKMonkey Nov 28 '17 at 13:41
  • Thank you sir, i'll do my best. – Marijke Buurlage Nov 28 '17 at 13:44

2 Answers2

3

You didn't say that you wanted your variables in the app namespace in app.cpp

#include "App.h"

using namespace std;

string App::FingerPrint = "";
string App::FingerPrintID = "";

should do the job

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
0

Right now, you just declare that somewhere, there are two strings in the namespace App. You never actually define those variables.

They don't end up in a namespace simply by being in a file with the same name as a namespace. (App.cpp has no relation with namespace App, it's just a convention).

So you either need to prepend App:: to your string declarations or wrap them in namespace App { ... } somewhere in a .cpp file.

That said: Global variables are evil. Try to define them in an App or Config or Model instance instead. During setup, create and initialize the instance and then pass it around as (constructor) parameter.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • @MarijkeBuurlage https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list is a good start – UKMonkey Nov 28 '17 at 13:30