Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. std::string constructor unresolved linking in VS 2010

std::string constructor unresolved linking in VS 2010

Scheduled Pinned Locked Moved ATL / WTL / STL
visual-studiohelpquestion
7 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    StevenS_Dev
    wrote on last edited by
    #1

    I am moving our project from VS 2005 to VS 2010 and am running up against some linker errors. std::string being multiply defined is one issue, but I will take some more time to look into that one. The one I am really confused about is the std::string constructor. We have been using

    // aCharArray is a char[]
    std::string abc(aCharArray, 20);
    // aStdString is a std::string
    std::string anotherExample(aStdString.c_str(), 20);

    without any problems. Now VS 2010 is giving me an unresolved external during linking. It is thinking I want the constructor std::string(const char*, unsigned int) which IS what I want. It used to be defined, and it does still seem to be defined. For some reason, updating to VS 2010, it is no longer linking, though. Is the implementation for this constructor perhaps missing? Anyone run into this or can anyone confirm the same issue? Thanks. Resolving these is pretty easy, but I am left confused as to why it gets unresolved external.

    S L 2 Replies Last reply
    0
    • S StevenS_Dev

      I am moving our project from VS 2005 to VS 2010 and am running up against some linker errors. std::string being multiply defined is one issue, but I will take some more time to look into that one. The one I am really confused about is the std::string constructor. We have been using

      // aCharArray is a char[]
      std::string abc(aCharArray, 20);
      // aStdString is a std::string
      std::string anotherExample(aStdString.c_str(), 20);

      without any problems. Now VS 2010 is giving me an unresolved external during linking. It is thinking I want the constructor std::string(const char*, unsigned int) which IS what I want. It used to be defined, and it does still seem to be defined. For some reason, updating to VS 2010, it is no longer linking, though. Is the implementation for this constructor perhaps missing? Anyone run into this or can anyone confirm the same issue? Thanks. Resolving these is pretty easy, but I am left confused as to why it gets unresolved external.

      S Offline
      S Offline
      StevenS_Dev
      wrote on last edited by
      #2

      I ran into something I don't understand. The following line is producing another unresolved link error. 'buf' is a std::string.

      std::getline(stream, buf,'\n');

      We have the same line of code in other projects that compile just fine. Below is the link error. To resolve the problem, I changed buf to a char[1024] and am calling stream.getline(buf, sizeof(buf), '\n'). But why am I getting this link error in the first place? The projects which compile fine are REQUIRED projects for the project giving me this problem. I don't know if the chain of links has something to do with this, but I thought I'd at least mention it.

      18>SimpleMCSFile.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator+=(char)" (__imp_??Y?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@D@Z) referenced in function "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl std::getline<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_istream<char,struct std::char_traits<char> > &&,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,char)" (??$getline@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@0@$$QAV10@AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@D@Z)

      S 1 Reply Last reply
      0
      • S StevenS_Dev

        I ran into something I don't understand. The following line is producing another unresolved link error. 'buf' is a std::string.

        std::getline(stream, buf,'\n');

        We have the same line of code in other projects that compile just fine. Below is the link error. To resolve the problem, I changed buf to a char[1024] and am calling stream.getline(buf, sizeof(buf), '\n'). But why am I getting this link error in the first place? The projects which compile fine are REQUIRED projects for the project giving me this problem. I don't know if the chain of links has something to do with this, but I thought I'd at least mention it.

        18>SimpleMCSFile.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator+=(char)" (__imp_??Y?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@D@Z) referenced in function "class std::basic_istream<char,struct std::char_traits<char> > & __cdecl std::getline<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_istream<char,struct std::char_traits<char> > &&,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,char)" (??$getline@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@YAAAV?$basic_istream@DU?$char_traits@D@std@@@0@$$QAV10@AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@0@D@Z)

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #3

        Looks like you're not linking to the library that contains the definition. First thing I'd do is create a new console project and see if it works there. Something like this should do:

        // Console.cpp : Defines the entry point for the console application.
        //

        #include "stdafx.h"
        #include <tchar.h>
        #include <string>
        #include <sstream>
        #include <iostream>

        int _tmain(int argc, _TCHAR* argv[])
        {
        using namespace std;

        // The source stream.
        string source = "First line\\nSecond line\\n";
        istringstream iss(source);
        
        // Print out stream line by line.
        string line;
        while (getline(iss, line))
        {
        	cout << line << endl;
        }
        
        cout << endl;
        
        return 0;
        

        }

        Steve

        1 Reply Last reply
        0
        • S StevenS_Dev

          I am moving our project from VS 2005 to VS 2010 and am running up against some linker errors. std::string being multiply defined is one issue, but I will take some more time to look into that one. The one I am really confused about is the std::string constructor. We have been using

          // aCharArray is a char[]
          std::string abc(aCharArray, 20);
          // aStdString is a std::string
          std::string anotherExample(aStdString.c_str(), 20);

          without any problems. Now VS 2010 is giving me an unresolved external during linking. It is thinking I want the constructor std::string(const char*, unsigned int) which IS what I want. It used to be defined, and it does still seem to be defined. For some reason, updating to VS 2010, it is no longer linking, though. Is the implementation for this constructor perhaps missing? Anyone run into this or can anyone confirm the same issue? Thanks. Resolving these is pretty easy, but I am left confused as to why it gets unresolved external.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I just compiled this with my copy of VS2010 and it compiles and links fine. There must be something else in your configuration that is causing the problem.

          It's time for a new signature.

          S 1 Reply Last reply
          0
          • L Lost User

            I just compiled this with my copy of VS2010 and it compiles and links fine. There must be something else in your configuration that is causing the problem.

            It's time for a new signature.

            S Offline
            S Offline
            StevenS_Dev
            wrote on last edited by
            #5

            Thanks everyone for the helpful input. It does seem like something strange with the project. I wrote a test console application and got no linker errors. And the same calls link in other projects within the Solution just fine. I compared projects and am unable to find a difference between a project that works and a project that does not work. I am now trying to clean up code. The pre-compiled headers have includes for a ton of STL includes. I removed the one for string and am converting the headers and CPP files appropriately to see what happens. Anyway, it does seem to be something odd with this project. Everything worked fine in VS 2005. We never upgraded to 2008 and now I'm converting to 2010. EDIT: As a result of this exercise, I did run across several headers which were also including string, so I was able to clean those up.

            modified on Monday, June 7, 2010 4:42 PM

            S 1 Reply Last reply
            0
            • S StevenS_Dev

              Thanks everyone for the helpful input. It does seem like something strange with the project. I wrote a test console application and got no linker errors. And the same calls link in other projects within the Solution just fine. I compared projects and am unable to find a difference between a project that works and a project that does not work. I am now trying to clean up code. The pre-compiled headers have includes for a ton of STL includes. I removed the one for string and am converting the headers and CPP files appropriately to see what happens. Anyway, it does seem to be something odd with this project. Everything worked fine in VS 2005. We never upgraded to 2008 and now I'm converting to 2010. EDIT: As a result of this exercise, I did run across several headers which were also including string, so I was able to clean those up.

              modified on Monday, June 7, 2010 4:42 PM

              S Offline
              S Offline
              Stephen Hewitt
              wrote on last edited by
              #6

              Compare the project files using a diff tool.

              Steve

              S 1 Reply Last reply
              0
              • S Stephen Hewitt

                Compare the project files using a diff tool.

                Steve

                S Offline
                S Offline
                StevenS_Dev
                wrote on last edited by
                #7

                Thanks again for the input. Took me nearly two weeks, but I finally figured out the problem. It turns out we had two classes derived from std::string. I did not realize we had classes derived from std::string. I had noticed in a smaller test case, adding a certain #include would show the problem. Removing the #include and things would compile/link just fine. Unfortunately, there is a HUGE trail of #include's in that one header. This led me to slowly move #include's out of headers into CPP files allowing me to narrow down which headers were causing a problem. Eventually I got things down to where I noticed one of the std::string-derived classes and I went, "Aha!". Changed it to a 'has a' and the problem went away.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups