Results 1 to 3 of 3

Thread: object returning const char* to QStrng

  1. #1
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question object returning const char* to QStrng

    Hi all,

    Sorry if this is n00b question.
    I'm working on some base classes now, and have a small class
    that prints to a QString.
    In a higher level class, I want to put that into a QString, but the
    compiler complains no matter what I try.

    Here's a simplified example

    class Dog
    {
    private:
    int age;
    public:
    Dog(int age);
    QString print(); // return dog age in QString
    };

    class ManyDogs
    {
    private:
    Dog1;
    Dog2;
    public:
    QString printdogs();
    }

    later:

    ManyDogs:rintdogs()
    {
    QString temp;
    temp = Dog1.print();
    }

    That last line won't compile with an error:
    "passing const Dog as 'this' argument of 'QString Dog.print()' discards qualifiers.

    And I tried returning "const char *" from print() also, with the same error.

    But similar code such as:
    printf("Dog age is %s\n". Dog1.print());
    works fine. Argh!

    Obviously something in the C++ object model I'm not getting.
    thanks in advance for any help.

    -Don

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: object returning const char* to QStrng

    You should use the const qualifiers in your methods.

    Qt Code:
    1. class Dog {
    2. //...
    3. QString print() const;
    4. //...
    5. };
    To copy to clipboard, switch view to plain text mode 

    etc.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Posts
    17
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: object returning const char* to QStrng

    Thanks! That worked great.

    <humbled again>

    -Don

Similar Threads

  1. Qy 4.4.3 MySQL driver failed
    By pamalite in forum Installation and Deployment
    Replies: 2
    Last Post: 23rd January 2010, 02:09
  2. shared vs static
    By alisami in forum Installation and Deployment
    Replies: 3
    Last Post: 4th October 2008, 14:04
  3. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 08:43
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 19:42
  5. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 20:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.