Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Linker freaking out about members in my contentmanager  (Read 929 times)

0 Members and 1 Guest are viewing this topic.

decooljb

  • Newbie
  • *
  • Posts: 1
    • View Profile
Linker freaking out about members in my contentmanager
« on: June 28, 2012, 05:41:17 am »
error LNK2001: unresolved external symbol "private: static int contentmanager::isize" (?isize@contentmanager@@0HA)

error LNK2001: unresolved external symbol "private: static class std::vector<class sf::Image,class std::allocator<class sf::Image> > contentmanager::idata" (?idata@contentmanager@@0V?$vector@VImage@sf@@V?$allocator@VImage@sf@@@std@@@std@@A)

error LNK2001: unresolved external symbol "private: static class std::map<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<struct std::pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const ,int> > > contentmanager::imap" (?imap@contentmanager@@0V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HU?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@std@@@2@@std@@A)
 

Link is preventing me from compiling my game due to the my contentmanager integration to both a .cpp and .h file.  I have no idea why, but my previous setup of the manager (just cpp) wouldn't compile at all. And, quite frankly, I have no idea how to read or interpret Linker errors.

The manager's designed to hold a map, vector, and integer. For each time getimage() is called, the manager checks the argument against the map and sees if there's an existing key. If there is, it would get the value of that key, then return the image from the vector via vector[value] (very much like an ID system.) Otherwise, it would load the image from file, save the ID (remember about the integer? it's used as the ID) in the map with the key as the argument, save the image into the vector (who's index is supposed to be the ID). The integer increments each time the method doesn't find the argument as a key in the map (so, the 'loading image from file path')

This is the C++ implementation, may you guys tell me what's wrong? I'm 100% sure something here is wrong, I just can't see what.

contentmanager.h
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <string>
using namespace std;

class contentmanager
{
public:
        static sf::Image getimage(string arg);

private:
        static map<string, int> imap;
        static vector<sf::Image> idata;
        static int isize;
};

contentmanager.cpp
#include "contentmanager.h"
using namespace std;

sf::Image contentmanager::getimage(string arg) {
        sf::Image image;
        string dir = "content/image/";
        dir.append(arg);
        if(contentmanager::imap.find(arg) == contentmanager::imap.end()) {
                contentmanager::imap[arg] = contentmanager::isize;
                if(!image.LoadFromFile(dir)) {
                        return image;
                } else {
                        contentmanager::imap[arg] = contentmanager::isize;
                        contentmanager::idata[contentmanager::isize] = image;
                        contentmanager::isize++;
                        return image;
                }
        } else {
                int iterator = contentmanager::imap[arg];
                return contentmanager::idata[iterator];
        }
}

Everything's meant to be static, as there'll be one unique instance of it for the game itself. I might change it to non-static and try to make it more object-oriented later, but this is how I'm starting out.

EDIT: I noticed I forgot to include contentmanager:: for some of the imap references to access the class's scope. I fixed that code, but I'm still getting those 3 linker errors.
« Last Edit: June 28, 2012, 05:45:33 am by decooljb »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Linker freaking out about members in my contentmanager
« Reply #1 on: June 28, 2012, 08:06:45 am »
This is a basic rule of C++. Search "static variable undefined reference" on Google, you'll find millions of valid answers.
Laurent Gomila - SFML developer

 

anything