Thread: Unresolved external symbol using STL vector reference

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    Unresolved external symbol using STL vector reference

    I get an "Unresolved external symbol" (LNK2019 in .NET) when I use a reference to STL vector as return type of a method. Both definition and declaration of method are in separated files. If I don't use a reference, all compile and link ok. What can be happend?

    The prototype of method (.h file):

    Code:
    class CON_CConsoleOutput
    {
    private:
        vector<string>  m_vsOutputLines;
    public:
        ...
        vector<string>& GetOutputLines();
        ...
    };

    The definition in cpp file:

    Code:
    vector<string>& CON_CConsoleOutput::GetOutputLines()
    {
        return m_vsOutputLines;
    }
    If I removed reference, it works. Could be a syntaxis problem?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Weird. When I run this it works fine:


    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class CON_CConsoleOutput
    {
    private:
        vector<string>  m_vsOutputLines;
    public:
        vector<string>& GetOutputLines();
    };
    
    vector<string>& CON_CConsoleOutput::GetOutputLines()
    {
        return m_vsOutputLines;
    }
    
    int main ( void )
    {
        CON_CConsoleOutput c;
        
        vector<string> vs = c.GetOutputLines();
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    Thanks for reply.

    Yes, if I create one file and write the class code in it, it works, like yours. Also works when I write the method inline in the class declaration:

    Code:
    class CON_CConsoleOutput
    {
    private:
        vector<string>  m_vsOutputLines;
    public:
        ...
        vector<string>& GetOutputLines() { return m_vsOuputLines; }
        ...
    };
    The reason what I don't do this way is that I have another methods in othre classes than have the same problem and that ones are very more complex... Have more than 20 lines

    The problem happends when I create a .h file and a .cpp file. It's like the compiler don't find the method when I use references.

    This is the error I get from linker:

    "error LNK2019: símbolo externo "public: class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > __thiscall CON_CConsoleOutput::GetOutputLines(void)" (?GetOutputLines@CON_CConsoleOutput@@QAE?AV?$vecto r@V?$basic_string@DU?$char_traits@D@std@@V?$alloca tor@D@2@@std@@V?$allocator@V?$basic_string@DU?$cha r_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@XZ ) sin resolver al que se hace referencia en la función "public: class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > __thiscall CON_CConsole::GetOutputLines(void)" (?GetOutputLines@CON_CConsole@@QAE?AV?$vector@V?$b asic_string@DU?$char_traits@D@std@@V?$allocator@D@ 2@@std@@V?$allocator@V?$basic_string@DU?$char_trai ts@D@std@@V?$allocator@D@2@@std@@@2@@std@@XZ)
    ../bin/debug/BattleArena.exe : fatal error LNK1120: 1 externos sin resolver"

    The compiler is Visual Studio .NET 2003

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The error doesn't seem to mention the reference. Are you sure you put the reference in the header declaration?

    An unresolved external sometimes happens when you forget to add the cpp file to the project that defines the function. It can also happen if you forget to add the CON_CConsoleOutput:: to the function definition.

    The code you posted looks correct, did you copy and paste it from your code or did you re-type it. If you re-typed it you might have just made a typo in the actual code.

    The reference should not affect the ability to link the function.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    I'm assuming you have:

    #include <vector>
    #include <string>
    and
    #include "CON_CConsoleOutput"
    in your files, right? And a using namespace std, of course, if you're not using std:: all over the place. Aside from that, if it still doesn't work perhaps you will have to give us more source to look at.

    con.h:
    Code:
    #include <string>
    #include <vector>
    
    using namespace std;
    
    class CON_CConsoleOutput
    {
    private:
        vector<string>  m_vsOutputLines;
    public:
        vector<string>& GetOutputLines();
    };
    con.cpp
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    #include "con.h"
    
    using namespace std;
    
    vector<string>& CON_CConsoleOutput::GetOutputLines()
    {
        return m_vsOutputLines;
    }
    should work, I think.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    When I compile
    Code:
    #include <vector>
    
    int main() {
        vector v;
    
        return 0;
    }
    with MSVC 7 (dinkumware), these are the errors I get.
    Code:
    sourceFile.cpp
    sourceFile.cpp(4) : error C2065: 'vector' : undeclared identifier
    sourceFile.cpp(4) : error C2146: syntax error : missing ';' before identifier 'v'
    sourceFile.cpp(4) : error C2065: 'v' : undeclared identifier
    Since you are getting linker errors, I'm guessing that for some reason either the code is not being linked with the STL (unlikely) or it has been disabled (some compilers allow this).

    Could you post the command-line arguments that are being passed to the compiler? It should be in the Compiler Options dialog box somewhere. If it has a "set to default" option, then try it (saving what it was before, of course). Also look for any options relating to the STL.

    Can you compile this simple program?
    Code:
    #include <vector>
    
    int main() {
        std::vector v;
    
        return 0;
    }
    If so, then the problem must lie elsewhere.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You need to specify a type for the vector's elements - for example,
    Code:
    #include <vector>
    
    int main() {
        std::vector<int> v;
    
        return 0;
    }
    will compile.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    An unresolved external sometimes happens when you forget to add the cpp file to the project that defines the function. It can also happen if you forget to add the CON_CConsoleOutput:: to the function definition.
    Yes, this is the first thing I verified (it's a common mistake). The code I post is a copy/paste from the project where I have the error. The same code without reference works ok

    I test the code in a blank project (con.cpp, con.h, main.cpp) and it works. So I think the problem is anywhere else.

    The compiler command-line arguments are:

    /Od /I "..\include" /I "C:\ZoidcomSDK\include" /I "C:\OGRE\OgreSDK\include" /I "C:\OGRE\OgreSDK\include\CEGUI" /I "C:\OGRE\OgreSDK\samples\include" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Gm /EHsc /RTC1 /MDd /Fo"Debug/" /Fd"Debug/vc70.pdb" /W3 /nologo /c /Wp64 /ZI /TP

    and manually add the option /GR (I'm using dynamic_cast elsewhere). All other options are autogenerated by setting options in project from IDE. Could be something relative to libs I'm using?

    Also I use STL in project (in a lot of places), and it works ok. The problem appears when I change the result value of method to a reference. This is the only error I get from STL, all compiles an links correctly. I never try to return a reference with STL, so I didn't know if this was an error mine or not.

    Thanks for your help

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That's very weird. If you remove the single '&' character from the header and the cpp file and rebuild it works, and if you put them back it fails?

    Since the code works separately with just those three files, you might consider reconstructing your project. Start with those three files in a new project, then add the other files from your old project and see if the new project builds. If possible, add the other files a few at a time (that might be hard to get the whole thing building to test the linking). If you can do that, and all of a sudden the error appears, then you'll have a better idea of any file that might be causing it if one is.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    That's very weird. If you remove the single '&' character from the header and the cpp file and rebuild it works, and if you put them back it fails?
    Yes, this is the weird thing. I don't know what's happening...

    Start with those three files in a new project, then add the other files from your old project and see if the new project builds. If possible, add the other files a few at a time
    This sound like a good idea. I will try to create a new project, and from there, go on adding new files and building each time. The problem is that I have a lot of files in the current project and a lot of dependencies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM