Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [SOLVED] QSring and Char
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QSring and Char

Scheduled Pinned Locked Moved C++ Gurus
5 Posts 2 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    when i run the program with the below code, i get an error: invalid conversion from 'const char*' to 'char'

    @char tt = "test";@

    yet when i change it too const char to fix the above error, i get the same error that i can't solve.

    @const char tt = "test";@

    I am trying to store "test" as a variable with char and its not working. I can store it with QString.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      I moved this to the C++ Gurus forum, as it is no Qt specific problem (and nothing "guru" too, but anyways...)

      You ran into a simple C syntax problem: char is a single character, so it's ok to write

      @
      const char tt = 'a';
      @

      but "test" is a string, so you need to assign it to a char array:

      @
      const char *tt = "test";
      @

      spot the asterisk before the tt - it makes tt a char array (= C string) as opposed to a single character without it.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #3

        thank you Volker.
        but why would i want to use char instead of Qstring?

        I basically got the answer to this above question when I ran the new code. warning: deprecated conversion from string constant to 'char*'. I guess char is deprecated and I should use QString instead.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Neither char nor char* are deprecated. This are valid C constructs and will never go away. Your error is most probably the fact that you use "char *" instead of "const char *". Again, this is basic C/C++ stuff.

          But you are right, in that you should avoid C strings (char *) whenever possible, because they are cumbersome to work with and you can easily run into trouble with the memory management.

          So, your code would look better this way:

          @
          // this calls the QString constructor with a const char * argument:
          QString tt2("abc");
          @

          If you have non-ASCII strings, consider one of the fromXXX static methods of [[Doc:QString]].

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kalster
            wrote on last edited by
            #5

            Thank you Volker. Yes, I forget to add the const to my code below which is why i got that deprecated warning message.

            I will do some more c++ studying.

            @char *tt = "test";@

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved