0

I'd like to access a static map that is stored in a seperate class and iterate through and print the contents.

So I have three files:

HasGlobals.cpp which has the global map.

/* HasGlobals.cpp */

#include <string>
#include <map>
#include <iostream>

class HasGlobals{
    public:
        static std::map<int, std::string> globalData;

        HasGlobals(){
            globalData.insert(std::pair<int, std::string>(0, "data"));
        };

        ~HasGlobals(){
        };
};

WantsGlobals.cpp which has a method that needs to use the map from HasGlobals.cpp for one of its methods.

/* WantsGlobals.cpp */

#include "HasGlobals.cpp"
#include <string>

class WantsGlobals{
    public:
        WantsGlobals(){
        };

        ~WantsGlobals(){
        };

        //THIS IS THE METHOD THAT I CAN'T GET TO WORK
        void printContentsOfGlobal(){
            HasGlobals * globalRetriever = new HasGlobals();
            for(std::map<int, std::string>::iterator it = globalRetriever->globalData.begin(); it != globalRetriever->globalData.end(); ++it){
                std::cout << it->first << " " << it->second << std::endl;
            }
        };
    };

Main.cpp which simply tries to call the printContentsOfGlobal() method in WantsGlobals.cpp.

/* Main.cpp */

#include <iostream>
#include "WantsGlobals.cpp"

int main(){
    WantsGlobals * globalRetriever = new WantsGlobals();
    globalRetriever->printContentsOfGlobal();
}

Right now this code doesn't compile and gives me

undefined reference to 'HasGlobals::globalData'

How would I implement printContentsOfGlobal() so that I can retrieve the data in the map[globalData] in the seperate HasGlobals.cpp file?

EDIT: Thanks for the help everyone. Below is the code I created with everyone's help. Yes it's still not perfect but it compiles and performs the desired behavior:

HasGlobals.hpp

#include <string>
#include <map>
#include <iostream>

class HasGlobals{
    public:
        static std::map<int, std::string> globalData;

        HasGlobals(){};

        ~HasGlobals(){};

        static std::map<int, std::string> createDummyData(){
            std::map<int, std::string> m;
            m[0] = "Help";
            m[1] = "Me";
            m[2] = "Stackoverflow";
            m[3] = "You're my only hope.";
            return m;
         }
};

std::map<int, std::string> HasGlobals::globalData = HasGlobals::createDummyData();

WantsGlobals.hpp

#include "HasGlobals.hpp"
#include <string>
#include <map>

class WantsGlobals{
    public:
        WantsGlobals(){};

        ~WantsGlobals(){};

        void printContentsOfGlobal(){
            for(std::map<int, std::string>::iterator it = HasGlobals::globalData.begin(); it != HasGlobals::globalData.end(); ++it){
                 std::cout << it->first << " " << it->second << std::endl;
             }
         };
};

Main.cpp

#include <iostream>
#include <"WantsGlobals.hpp">

int main(){
    WantsGlobals * globalRetriever = new WantsGlobals();
    globalRetriever->printContentsOfGlobal();
}
CSLearner
  • 249
  • 1
  • 5
  • 17
  • You have a typo btw. `std:;string` – Millie Smith Jul 05 '17 at 16:03
  • Are you asking how you can access a static class variable? – Rakete1111 Jul 05 '17 at 16:03
  • This compiles for me once I fix the typo. What specifically is your problem? – Millie Smith Jul 05 '17 at 16:05
  • Thanks Millie, I'll edit that. Yes Rakete1111, my goal is to access the static class variable from a seperate file. The codebase I'm using is larger than the example I'm posting so I'll be including .h files in the actual codebase I'm working on. #include ".cpp" is just a placeholder basically right now. – CSLearner Jul 05 '17 at 16:05
  • Right now this code doesn't compile and gives me: "undefined reference to 'HasGlobals::globalData'. Basically the entire printContentsOfGlobal() method is total garbage right now. – CSLearner Jul 05 '17 at 16:07
  • The link is helpful but I'm not working with a static integer so there are some small differences between my problem and the link. Thank you for sharing of course. – CSLearner Jul 05 '17 at 16:27
  • Would I define the static member in WantsGlobal.cpp? – CSLearner Jul 05 '17 at 16:50

1 Answers1

4

(CW as this is now just hints & tips on a dupe, left for posterity.)

Why are you #includeing .cpp files? Don't do that. It looks like those two files should be named .hpp instead.

You're close. Since the map is static, you don't need an instance of HasGlobals to access it, so HasGlobals * globalRetriever = new HasGlobals(); is pointless (plus you leaked it).

The map is "called" HasGlobals::globalData.

Though you will need to find some place to add data to it; the HasGlobals constructor is not the proper place, as that is called every time you instantiate the class. Best to use the initialiser when you define the object, which you forgot to do, hence the "undefined reference" linker error you mentioned in comments.

You are using the map correctly from then on, except from a typo here:

std::map<int, std:;string>
//                ^
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I'd guess an instance of the globals was created to put something in the map, looking at the code. Doing so on the heap was asking for a memory leak though. – doctorlove Jul 05 '17 at 16:10
  • @doctorlove: Yep. OP can initialise at the point of definition instead. – Lightness Races in Orbit Jul 05 '17 at 16:10
  • To clarify, the code above is meant as an example of the actual problem I'm trying to fix while isolating the parts I felt were relevant. I've tried implementing the changes you suggested but the code still isn't compiling. – CSLearner Jul 05 '17 at 16:25
  • Right now the print statements look like this: for(std::map::iterator it = HasGlobals::globalData.begin(); it != HasGlobals::globalData.end(); ++it){ std::cout << it->first << " " << it->second << std::endl; } For the defining would I put the definition in the WantsGlobals.cpp file? – CSLearner Jul 05 '17 at 16:49
  • The duplicate has a single file while mine is 3 seperate files for this specific example which is why I inquired which specific file requires the definition. I am not sure which book you're referencing. – CSLearner Jul 05 '17 at 17:10
  • Thank you for all your help! I was able to get it working! – CSLearner Jul 06 '17 at 18:34
  • @CSLearner: That code is _much_ better now. – Lightness Races in Orbit Jul 06 '17 at 18:59