0

I hope my question is not to localized... But I think some other will stumble about this or a similar problem.

I want to create a vector which contains all available ports at my system (this works in a console app). After that I want to copy the vectorelements in a wxString-Array to get them in a wxComboBox.

So, in my particular case I get two errors:

  1. the variablename of the vector is not known in wxWidgets
  2. by copying, the wxString will cast my string into a wchar_t (I know, wchar_t and wxString are similar...)

I will add some of my Code, so you will have a better sight about the problem:

first problem

std::vector<std::string> v_ports;
v_ports.push_back = "Com1";
v_ports.push_back = "Com4";

--> error: 'v_ports' does not name a type (hint: that is a example, in the hole programm I will use a function to get the strings)

second problem

wxString sect_opt[v_ports.size()];

for(int i = 0; i < v_ports.size(); i++)
    sect_opt[i] = _T(v_ports[i]);

--> error: 'Lv_ports' was not declared in this scope

I'm using: IDE: CodeLite 5.1; wxW 2.9.4; @Win8.1

Shoe
  • 74,840
  • 36
  • 166
  • 272
Casisto
  • 41
  • 4
  • try `v_ports.push_back("Com1");` – Deqing May 30 '14 at 07:11
  • O my God... I must have get to less sleep... but it's doesn't fix the problem – Casisto May 30 '14 at 07:18
  • nothing wrong with your first problem now, see: http://ideone.com/s5LRGx – Deqing May 30 '14 at 07:35
  • @billz: yes, both headers are included – Casisto May 30 '14 at 08:14
  • @Dequing: if you has try it out in a console-app you will get no error, I've first create my function as console-app, but when I try to use the function with wxW the error raise. – Casisto May 30 '14 at 08:16
  • Why are you using `_T()` wrapped around a **variable** ?? It's a literal-only wrapper for prepending (or not) `L` to character and string **literals** only, when building Unicode (or not) programs. Lose that wrapper and try `v_ports[i].c_str()` in that statement instead. – WhozCraig May 30 '14 at 08:59

1 Answers1

3

First problem

Instead of using:

v_ports.push_back = "Com1";
v_ports.push_back = "Com4";

you should use:

v_ports.push_back("Com1");
v_ports.push_back("Com4");

because std::vector<T>::push_back is a function.

Second problem

The _T macro is supposed to be used on literals:

Use the _T macro to conditionally code literal strings to be portable to Unicode.

It cannot be used in expressions like _T(v_ports[i]).

To convert a string to unicode please see:

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • Reason for the downvote? I'd love to improve my answer if there's something wrong with it. – Shoe May 30 '14 at 10:56
  • 1
    `std::string` can be converted to `wxString` (doing the Unicode conversion in the process implicitly) directly, i.e. just writing `wxString(v_ports[i])` is enough if the string is ASCII (or encoded in the current locale encoding). Otherwise `wxString::FromUTF8()` could be useful. – VZ. May 30 '14 at 13:13
  • Hi, and thanks, but how I write to Deqing, the error is still there, even when i use push_back as function. (I had forgot to update it here, I had must to tired this morning when i used push_back like above...) But the second problem is fixed now, thx. – Casisto May 30 '14 at 15:38
  • @Casisto Please don't update your question with the solution. Leave it as it is. The solution stays in the answers. – Shoe May 30 '14 at 16:31