Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Comparison between distinct pointer types 'QString*' and 'const char*' lacks a cast [-fpermissive]

Comparison between distinct pointer types 'QString*' and 'const char*' lacks a cast [-fpermissive]

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 9.2k Views
  • 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.
  • I Offline
    I Offline
    Imhotep
    wrote on 9 Jul 2014, 11:51 last edited by
    #1

    I had a QString inizialized in the .h file

    @
    QString *str;
    @

    In the .cpp constructor, str id declared

    @
    active = new QString;
    if (str->isEmpty()) { *str = "foobar"; }
    @

    Now, when I try to set an int variable in this way

    @
    int num = (str == "foobar") ? 5: 6;
    @

    Qt Creator sends me this error:

    "comparison between distinct pointer types 'QString*' and 'const char*' lacks a cast [-fpermissive]"

    I guess that I have not run a cast, but what exactly?
    Could you help me?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      p3c0
      Moderators
      wrote on 9 Jul 2014, 12:11 last edited by
      #2

      Hi,

      You should dereference str using * operator during comparison.
      This will work,
      @
      int num = (*str == "foobar") ? 5: 6;
      @

      157

      1 Reply Last reply
      0
      • I Offline
        I Offline
        Imhotep
        wrote on 9 Jul 2014, 12:15 last edited by
        #3

        [quote author="p3c0" date="1404907893"]Hi,

        You should dereference str using * operator during comparison.
        This will work,
        @
        int num = (*str == "foobar") ? 5: 6;
        @[/quote]

        The program crash on start

        1 Reply Last reply
        0
        • P Offline
          P Offline
          p3c0
          Moderators
          wrote on 9 Jul 2014, 12:18 last edited by
          #4

          In your first post i see you have initialised active but not str. Is that a typo ?

          157

          1 Reply Last reply
          0
          • I Offline
            I Offline
            Imhotep
            wrote on 9 Jul 2014, 12:22 last edited by
            #5

            No, i misswrited the variable name. str is correct

            1 Reply Last reply
            0
            • P Offline
              P Offline
              p3c0
              Moderators
              wrote on 9 Jul 2014, 12:25 last edited by
              #6

              So is it still crashing after initialising str ? I mean after doing this,
              @
              QString *str = new QString;
              if (str->isEmpty()) { *str = "foobar"; }
              int num = (*str == "foobar") ? 5: 6;
              @

              157

              1 Reply Last reply
              0
              • I Offline
                I Offline
                Imhotep
                wrote on 9 Jul 2014, 12:36 last edited by
                #7

                [quote author="p3c0" date="1404908755"]So is it still crashing after initialising str ? I mean after doing this,
                @
                QString *str = new QString;
                if (str->isEmpty()) { *str = "foobar"; }
                int num = (*str == "foobar") ? 5: 6;
                @[/quote]

                Solved

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  p3c0
                  Moderators
                  wrote on 9 Jul 2014, 12:43 last edited by
                  #8

                  Good. You can mark the post as solved.

                  157

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 9 Jul 2014, 22:00 last edited by
                    #9

                    Hi,

                    Out of curiosity, why are you using a pointer to a QString ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Chastain
                      Banned
                      wrote on 27 Mar 2018, 12:00 last edited by
                      #10
                      This post is deleted!
                      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