Linking errors with static var

This is a discussion on Linking errors with static var within the C++ Programming forums, part of the General Programming Boards category; Just a quick question here, because I haven't had time to figure it out yet. Here are the errors: Code: ...

  1. #1
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167

    Linking errors with static var

    Just a quick question here, because I haven't had time to figure it out yet.
    Here are the errors:

    Code:
    4>Kantan AnimeDlg.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<char>::PointerInfo *,struct CMemoryManager<char>::PointerInfo *> CMemoryManager<char>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@D@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@D@@PAU12@@@A)
    4>Kantan AnimeDlg.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CFileMap>::PointerInfo *,struct CMemoryManager<class CFileMap>::PointerInfo *> CMemoryManager<class CFileMap>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCFileMap@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCFileMap@@@@PAU12@@@A)
    4>Kantan AnimeDlg.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CAudioProfile>::PointerInfo *,struct CMemoryManager<class CAudioProfile>::PointerInfo *> CMemoryManager<class CAudioProfile>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCAudioProfile@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCAudioProfile@@@@PAU12@@@A)
    4>Kantan AnimeDlg.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CError>::PointerInfo *,struct CMemoryManager<class CError>::PointerInfo *> CMemoryManager<class CError>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCError@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCError@@@@PAU12@@@A)
    4>ExtractVideo.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CError>::PointerInfo *,struct CMemoryManager<class CError>::PointerInfo *> CMemoryManager<class CError>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCError@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCError@@@@PAU12@@@A)
    4>Kantan AnimeDlg.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CKantanAnimeDlg::CError>::PointerInfo *,struct CMemoryManager<class CKantanAnimeDlg::CError>::PointerInfo *> CMemoryManager<class CKantanAnimeDlg::CError>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCError@CKantanAnimeDlg@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCError@CKantanAnimeDlg@@@@PAU12@@@A)
    4>Kantan AnimeDlg.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CAskDefaultAction>::PointerInfo *,struct CMemoryManager<class CAskDefaultAction>::PointerInfo *> CMemoryManager<class CAskDefaultAction>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCAskDefaultAction@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCAskDefaultAction@@@@PAU12@@@A)
    4>Kantan AnimeDlg.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CChooseStream>::PointerInfo *,struct CMemoryManager<class CChooseStream>::PointerInfo *> CMemoryManager<class CChooseStream>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCChooseStream@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCChooseStream@@@@PAU12@@@A)
    4>Kantan AnimeDlg.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CKantanAnimeDlg::CGUITask>::PointerInfo *,struct CMemoryManager<class CKantanAnimeDlg::CGUITask>::PointerInfo *> CMemoryManager<class CKantanAnimeDlg::CGUITask>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCGUITask@CKantanAnimeDlg@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCGUITask@CKantanAnimeDlg@@@@PAU12@@@A)
    4>Kantan Anime.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CKantanAnimeDlg::CGUITask>::PointerInfo *,struct CMemoryManager<class CKantanAnimeDlg::CGUITask>::PointerInfo *> CMemoryManager<class CKantanAnimeDlg::CGUITask>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCGUITask@CKantanAnimeDlg@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCGUITask@CKantanAnimeDlg@@@@PAU12@@@A)
    4>Kantan Anime.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CRect>::PointerInfo *,struct CMemoryManager<class CRect>::PointerInfo *> CMemoryManager<class CRect>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCRect@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCRect@@@@PAU12@@@A)
    4>AvisynthWriter.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CAvisynthWriter::CError>::PointerInfo *,struct CMemoryManager<class CAvisynthWriter::CError>::PointerInfo *> CMemoryManager<class CAvisynthWriter::CError>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCError@CAvisynthWriter@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCError@CAvisynthWriter@@@@PAU12@@@A)
    4>CLIWrapper.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CCLIWrapper::CError>::PointerInfo *,struct CMemoryManager<class CCLIWrapper::CError>::PointerInfo *> CMemoryManager<class CCLIWrapper::CError>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCError@CCLIWrapper@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCError@CCLIWrapper@@@@PAU12@@@A)
    4>ConvertFile.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CCLIWrapper::CError>::PointerInfo *,struct CMemoryManager<class CCLIWrapper::CError>::PointerInfo *> CMemoryManager<class CCLIWrapper::CError>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCError@CCLIWrapper@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCError@CCLIWrapper@@@@PAU12@@@A)
    4>ExtractAudio.obj : error LNK2001: unresolved external symbol "protected: static class CMap<void *,void *,struct CMemoryManager<class CExtractAudio::CError>::PointerInfo *,struct CMemoryManager<class CExtractAudio::CError>::PointerInfo *> CMemoryManager<class CExtractAudio::CError>::m_InfoMap" (?m_InfoMap@?$CMemoryManager@VCError@CExtractAudio@@@@1V?$CMap@PAXPAXPAUPointerInfo@?$CMemoryManager@VCError@CExtractAudio@@@@PAU12@@@A)
    This is no doubt because the class CMemoryManager tries to access the static variable:
    Code:
    	struct PointerInfo
    	{
    		DWORD* pdwRefCount;
    		bool bArray;
    		bool bThreadSafe;
    		CCriticalSection cSync;
    	};
    
    	T* p;
    	PointerInfo* m_pInfo;
    	static CMap<void*, void*, PointerInfo*, PointerInfo*> m_InfoMap;
    It tries to access m_InfoMap, but the linker says it can't find the symbol.
    Example code:

    Code:
    		CMemoryManager::m_InfoMap.Lookup(p, pInfo);
    		CMemoryManager::m_InfoMap.SetAt(p, pInfo);
    Errors like this are usually a pain to figure out and I don't have time to figure it out right so I thought I could give you all a chance

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Elysia View Post
    I don't have time to figure it out right so I thought I could give you all a chance
    That's really nice of you. .... guess I have figured it out.
    Kurt

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Please tell me then

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Looks like you have just declared the static var but not created it in any of the implementation files.
    Kurt

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Oh? I'm not sure how to create such a variable in an .cpp file.
    I don't think I've made a static var like this before...
    The entire file is also a template class, declared (and defined) in the header file.
    Following doesn't work:
    Code:
    template<typename T> static CMap<void*, void*, typename CMemoryManager<T>::PointerInfo*, typename CMemoryManager<T>:PointerInfo*> CMemoryManager<T>::m_InfoMap;
    Last edited by Elysia; 10-27-2007 at 04:16 PM.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Try something like this

    Code:
    #include <map>
    using std::map;
    
    template <class T>
    class test {
    public:
        void set(int idx, const T & t ) { statmap[idx]=t; }
    private:
        static map<int, T> statmap;
    };
    
    template<class T>
    map<int, T> test<T>::statmap;
    
    int main() {
        test<int> ti;
        ti.set(3, 4);
    }
    Kurt

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    The compiler (or linker) is toying with me - I can't even get a simple static int to work:

    Code:
    template<typename T> class CMemoryManager
    {
    	static int test;
    	...
    };
    
    template<typename T>
    int CMemoryManager<T>::test;
    Linker errors. Again.
    Unless... might the problem be because I'm accessing the static var from the header?

    No... it seems more like it's compiling the .cpp file but refusing to accept the contents inside. Tried moving a function to the .cpp file and not it complains it can't find that function.
    Templates can be so annoying.

    Code:
    #include "stdafx.h"
    #include "MemoryManager.h"
    #pragma message("Compiling MemoryManager.cpp...")
    
    //template<typename T> CMap<void*, void*, typename CMemoryManager<T>::PointerInfo*, typename CMemoryManager<T>::PointerInfo*> CMemoryManager<T>::m_InfoMap;
    
    template<typename T>
    int CMemoryManager<T>::test;
    
    template<typename T>
    void CMemoryManager<T>::Init()
    {
    	//pdwRefCount = NULL;
    	CheckPointerInfo();
    	p = NULL;
    	CMemoryManager::test = 0;
    	//bArray = false;
    	//bThreadSafe = false;
    }
    Code:
    template<typename T> class CMemoryManager
    {
    protected:
    	...
    	void Init();
    	...
    };
    Last edited by Elysia; 10-27-2007 at 05:20 PM.

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You can keep the definition of the static variable in the header (outside the class declaration ). Most compilers nowadays should handle that allright.
    Kurt

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Quote Originally Posted by ZuK View Post
    You can keep the definition of the static variable in the header (outside the class declaration ). Most compilers nowadays should handle that allright.
    Kurt
    YES! That does the trick!
    Thanks a bunch, cudos to you and a big finger to the compiler.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 1
    Last Post: 03-30-2004, 01:57 PM
  4. help with allegro - linking errors
    By MadHatter in forum C++ Programming
    Replies: 1
    Last Post: 11-22-2002, 01:01 PM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21