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

6
  • try v_ports.push_back("Com1"); Commented May 30, 2014 at 7:11
  • O my God... I must have get to less sleep... but it's doesn't fix the problem Commented May 30, 2014 at 7:18
  • nothing wrong with your first problem now, see: ideone.com/s5LRGx Commented May 30, 2014 at 7:35
  • @billz: yes, both headers are included Commented May 30, 2014 at 8: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. Commented May 30, 2014 at 8:16

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

4 Comments

Reason for the downvote? I'd love to improve my answer if there's something wrong with it.
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.
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 Please don't update your question with the solution. Leave it as it is. The solution stays in the answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.