0

I have three files:

TableGenerataor.h
TableGenerataor.cpp
Analyzer.cpp

I have declared unordered_map in TableGenerator.h and using that I create the object of unorderd_map in TableGenerataor.cpp.

map_t obj1;
data d;

TableGenerator.h

namespace N {

typedef std::tuple<int, char> key_t;

struct key_hash : public std::unary_function<key_t, std::size_t>
{
std::size_t operator()(const key_t& k) const
{
return std::get<0>(k) ^ std::get<1>(k) ;
}
};

struct key_equal : public std::binary_function<key_t, key_t, bool>
{
bool operator()(const key_t& v0, const key_t& v1) const
{
return (
std::get<0>(v0) == std::get<0>(v1) &&
std::get<1>(v0) == std::get<1>(v1) 
);
}
};

struct data
{
int AlertNo;

};

typedef std::unordered_map<const key_t,data,key_hash,key_equal> map_t;

}

enter code here

I want to use this unordered_map created in TableGenerator.cpp file in Analyzer.cpp. I want to keep this unordered_map till the end of the program. For that purpose I want to make unordered_map global. Global seems to be better option rather than sending map as parameter to other function. I use the keyword extern in Analyzer.cpp.

extern map_t obj1; extern data d;

but it still says that undefined reference to obj1 when compiling. Please help me how do I make unorderd_map Global to make use of it in other cpp files. Secondly, is their any other better way to do this thing rather than making map global.

Xara
  • 8,748
  • 16
  • 52
  • 82
  • 2
    I don't think this has anything to to with the global being an `unordered_map`. – juanchopanza Jan 09 '14 at 06:53
  • @juanchopanza I edited my question, while compiling it says undefined reference to obj1. This error I get when I link the object files. – Xara Jan 09 '14 at 07:04
  • My previous statement still holds. – juanchopanza Jan 09 '14 at 07:08
  • To answer your second question, instead of making your variable global you could wrap it in a Singleton class http://stackoverflow.com/a/1008289/3005167 – MB-F Jan 09 '14 at 07:30

2 Answers2

0

Sounds like you have a syntax issue.

Consult these resources to see some examples:

Community
  • 1
  • 1
Keeler
  • 2,102
  • 14
  • 20
0

Are variables declarations in the same namespace as their definition? E.g. N::obj1 might have been defined butobj1 is referred to.


Original Answer:

You wrote in Analyzer.cpp:

extern map_t obj1;
extern data d;

So I assume you have

#include "TableGenerator.h"

in "Analyzer.cpp".

Move declarations of the two global variables to "TableGenerator.h". (Keep the definition of them in "TableGenerator.cpp".)

And try to access N::obj1 and N::d in "Analyzer.cpp".

edwardtoday
  • 363
  • 2
  • 14