Author Topic: Why is this wrong?  (Read 1428 times)

Offline Avan

  • Multiple posting newcomer
  • *
  • Posts: 24
Why is this wrong?
« on: March 25, 2021, 01:23:01 pm »
Why is this a binary operation?
Code
wxString Dir = _T("X:\\") + _T("Foldername");

error: invalid operands of types 'const wchar_t [4]' and 'const wchar_t [9]' to binary 'operator+'
And why do these proper string concats not ptoduce the same error?
Code
wxString name = _T("Foldername");
wxString Dir  = _T("H:\\") + name;
wxString Dir  = file.GetPath() + _T("Foldername");

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2987
Re: Why is this wrong?
« Reply #1 on: March 25, 2021, 01:52:25 pm »
In general this is not a general programming forum. This is a codeblocks forum, so this is the wrong place for this question.

Disclaimer, this is more pseudo code than actual code:
In your case i think _T defines to something like this
Code
#define _T(n) _(n) 
or something like this
Code
#define _T(n) _TCHAR(n)
what translates to
Code
#define _TCHAR(n) wchar_t(n)

so your final line of code is
Code
wxString Dir ={'X', ':' + '\\'} + {'F','o','l','d','e','r','n','a','m','e'};
what is like

Code
wxString Dir = char[4] + char[11];
and adding arrays is not defined...
what is a binary operation, normally



Offline Avan

  • Multiple posting newcomer
  • *
  • Posts: 24
Re: Why is this wrong?
« Reply #2 on: March 25, 2021, 03:08:14 pm »
Ah, I was wondering why there were [n] brackets but it didn't sink in.

It's actually:
#define _T(x) __T(x)

Solved it with:
Code
wxString Dir.append(_T("X:\\"), _T("Foldername"));
Edit: Spoke too soon. The compiler didn't complain but appending two stings this way crash CB when executed. I had to use an assign and then append the next.

An intermediate variable also did the trick.

Thanks, and I will NEVER ask for help in the Help forum again. What w.a.s I thinking... ;D
« Last Edit: March 25, 2021, 07:06:13 pm by Avan »

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 2987
Re: Why is this wrong?
« Reply #3 on: March 25, 2021, 05:26:42 pm »
Quote
Thanks, and I will NEVER ask for help in the Help forum again. What w.a.s I thinking...
Every question related to codeblocks is welcome :)

if you are using wx3.... you can omit the _T part, if you do not plan to translate your application.

Offline Avan

  • Multiple posting newcomer
  • *
  • Posts: 24
Re: Why is this wrong?
« Reply #4 on: March 25, 2021, 10:24:14 pm »
Quote
if you are using wx3.... you can omit the _T part, if you do not plan to translate your application.

Good to know.

Offline Miguel Gimenez

  • Lives here!
  • ****
  • Posts: 754
Re: Why is this wrong?
« Reply #5 on: March 25, 2021, 10:34:40 pm »
Translation uses _().