Thread: Dont understand error message

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    Dont understand error message

    I get an error message on the second line and I dont understand why. Shouldnt this code compile?

    Code:
    wstring s(FileName, FileNameLength);
    s = wstring(L"\\") + s;
    the message is:

    1>d:\dev\projects\dirwatcher\dirwatcher\dirwatcher .cpp(27) : error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std:perator +(_String_iterator<_Elem,_Traits,_Alloc>::differen ce_type,std::_String_iterator<_Elem,_Traits,_Alloc >)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from 'std::wstring'

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Did you include <string>?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    lol that was the problem. been a while since I didnt understand such a basic issue. Strange though that it did work when I rewrote it to the following without including string either.

    Code:
    wstring temp;
    wstring s(fni.FileName, fni.FileNameLength);
    temp += wstring(Dir);
    temp += L"\\";
    temp += s;

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That's because += is a member of std::basic_string and thus available when an internal component of the MS library include <xstring>, whereas + is a free function defined in <string>, and thus not available.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    That's because += is a member of std::basic_string and thus available when an internal component of the MS library include <xstring>, whereas + is a free function defined in <string>, and thus not available.
    And this implies that <xstring> gets included by some other component at some point or another - you STILL SHOULD include <string> since you are using std:w)string - it may work as it is, but it's a bad idea to rely on it, as a different set of include files may drag in less "extras" and thus break your build.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed