0

I have this function that return a string:

string getStringFromFile()
    {
    ifstream in("in.txt");
        ofstream out("out.txt");
        std::string line;
        while(getline(in,line)){
        if(line.empty())
              line="http://www.google.com/";
        }

        return line;
    }

When trying to call the getStringFromFile function, it return a syntax error(Error : identificator not defined).

CreateWindowEx(0, _T("EDIT"),
                       _T(getStringFromFile()),
                       WS_CHILD | WS_VISIBLE | WS_BORDER,
                       260, 10,
                       200, 20,
                       hWnd, NULL, hInst, NULL);
Kamel
  • 243
  • 2
  • 17
  • `_T()` is a macro, that can be used with character string literals only. – πάντα ῥεῖ Jul 21 '15 at 16:52
  • @Mr1Penguin: The inability to comment below 10 reputation is designed to encourage you to _watch and learn_ before commenting, so that you do not comment inappropriately before you know how the site works. Ironically, you've done almost the opposite by taking your comment and instead writing it as an answer, which is not the correct course of action. It is self-evident that this is not the right thing to do ("I know it's illegal, officer, _but_...!"). Belated welcome to Stack Overflow! Also, I like your name. – Lightness Races in Orbit Jul 21 '15 at 16:57

3 Answers3

5

You cannot use _T() like this. It's a macro that applies the L suffix to string literals, not a function that can be applied to arbitrary expressions.

You should start with the wide-string versions of those library types, which is essentially the equivalent of what you are trying to hack in with the _T():

  • std::wifstream
  • std::wofstream
  • std::wstring

It also seems likely, as you are calling a Windows API function, that you will have to obtain a pointer to the string data rather than trying to pass an actual C++ string object.

  • getStringFromFile().c_str()

This'll give you the wchar_t const* that you need though, to be honest, I'm not entirely sure whether it's safe to do this with a temporary. It depends on what the preconditions are for CreateWindowEx.

Safest approach, IMO:

std::wstring getStringFromFile()
{
    std::wifstream in("in.txt");
    std::wofstream out("out.txt");
    std::wstring line;

    while (std::getline(in,line)) {
       if (line.empty())
          line = "http://www.google.com/";
    }

    return line;
}

// ...

const std::wstring str = getStringFromFile();
CreateWindowEx(0, _T("EDIT"),
               str.c_str(),
               WS_CHILD | WS_VISIBLE | WS_BORDER,
               260, 10,
               200, 20,
               hWnd, NULL, hInst, NULL
);

As a further complication, the _T() macro only applies the L suffix to string literals when your program has UNICODE defined. If you wish to support both Unicode and non-Unicode modes, you'll need to switch back to the original std::string etc when UNICODE is not defined. That being said, I've been led to believe that disabling UNICODE is rare nowadays. Still, I'm no Windows dev…

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
3

You cannot call the function within the _T macro

That macro expands to (possibly) append the L to the string literal. Your function returns a std::string, so you cannot modify it as if it were a literal.

To be clear, that would be like having the function

int foo()
{
    return 5;
}

And trying to call it as

foo().0

This will obviously not convert your int to a double just because you added the .0 which will specify a double literal

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
3

_T is a macro used to handle literal strings either as UNICODE or ANSI strings depending on your project's settings. If you want to support both, you have to code your getStringFromFile() function accordingly by returning a std::wstring under your UNICODE build and calling the .c_str() method to pass the value to the CreateWindowEx function.

Sebacote
  • 690
  • 6
  • 17