Link time errors for static members
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Link time errors for static members

  1. #1
    Join Date
    Dec 2010
    Posts
    642

    Link time errors for static members

    Code:
        class NavMeshLoader
        {
        public:
                NavMeshLoader()
                {
                        m_navMesh = loadAll("all_tiles_navmesh.bin");
                        m_navQuery->init(m_navMesh, 2048);
                }
        public:
                dtNavMesh *loadAll(const std::string& path)
                {
                        FILE* fp = fopen(path.c_str(), "rb");
                        if (!fp) return 0;
               
                        // Read header.
                        NavMeshSetHeader header;
                        fread(&header, sizeof(NavMeshSetHeader), 1, fp);
                        if (header.magic != NAVMESHSET_MAGIC)
                        {
                                fclose(fp);
                                return 0;
                        }
                        if (header.version != NAVMESHSET_VERSION)
                        {
                                fclose(fp);
                                return 0;
                        }
               
                        dtNavMesh* mesh = dtAllocNavMesh();
                        if (!mesh)
                        {
                                fclose(fp);
                                return 0;
                        }
                        dtStatus status = mesh->init(&header.params);
                        if (dtStatusFailed(status))
                        {
                                fclose(fp);
                                return 0;
                        }
                       
                        // Read tiles.
                        for (int i = 0; i < header.numTiles; ++i)
                        {
                                NavMeshTileHeader tileHeader;
                                fread(&tileHeader, sizeof(tileHeader), 1, fp);
                                if (!tileHeader.tileRef || !tileHeader.dataSize)
                                        break;
    
                                unsigned char* data = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM);
                                if (!data) break;
                                memset(data, 0, tileHeader.dataSize);
                                fread(data, tileHeader.dataSize, 1, fp);
                       
                                mesh->addTile(data, tileHeader.dataSize, DT_TILE_FREE_DATA, tileHeader.tileRef, 0);
                        }
               
                        fclose(fp);
               
                        return mesh;
                }
    
        private:
                // By reference only, no memory consumption
                static dtNavMesh *m_navMesh;
                          
                static dtNavMeshQuery* m_navQuery;
    
                static dtQueryFilter m_filter;
    
                static dtStatus m_pathFindStatus;
        };
    Error 3 error LNK2001: unresolved external symbol "private: static class dtNavMesh * NavMeshLoader::m_navMesh" (?m_navMesh@NavMeshLoader@@0PAVdtNavMesh@@A) D:\Jacky\Documents\Visual Studio 2010\Projects\PerfectSim\PerfectSim\PerfectSim\main.obj PerfectSim
    Error 2 error LNK2001: unresolved external symbol "private: static class dtNavMeshQuery * NavMeshLoader::m_navQuery" (?m_navQuery@NavMeshLoader@@0PAVdtNavMeshQuery@@A) D:\Jacky\Documents\Visual Studio 2010\Projects\PerfectSim\PerfectSim\PerfectSim\main.obj PerfectSim

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,347

    Re: Link time errors for static members

    Did you define these static members in exactly one source file?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Dec 2010
    Posts
    642

    Re: Link time errors for static members

    Hi laserlight,
    I defined these static members in a file called NavMeshLoader.h. Just defined in this file.
    Jack

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,347

    Re: Link time errors for static members

    Quote Originally Posted by lucky6969b
    I defined these static members in a file called NavMeshLoader.h. Just defined in this file.
    That is wrong because a header could be included in multiple source files, or not at all. My guess is that the latter happened here, hence the linker cannot find the definitions of these static member variables.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: Link time errors for static members

    Quote Originally Posted by lucky6969b View Post
    Hi laserlight,
    I defined these static members in a file called NavMeshLoader.h. Just defined in this file.
    Jack
    You did not define them. You only declared them.
    Now you have to define them. Something like:
    Code:
    NavMeshLoader::dtNavMeshQuery* m_navQuery = NULL;
    NavMeshLoader::dtStatus m_pathFindStatus;
    ...
    and so on.
    Usually you do it in some of the implementation (.cpp) files.
    See http://www.tutorialspoint.com/cplusp...ic_members.htm
    Victor Nijegorodov

  6. #6
    Join Date
    Dec 2010
    Posts
    642

    Re: Link time errors for static members

    Yes, I just forgot that.
    Thanks for refreshing my memory
    Jack

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center