Results 1 to 2 of 2

Thread: Using toString() of another class

  1. #1
    Join Date
    Aug 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Using toString() of another class

    Hi,

    Ive got 2 classes and I would like Class2 to use the toString() of Class1, but Im getting the following error:

    error: cannot call member function 'QString Process::toString() const' without object
    .arg(Process::toString());
    ^
    Class1:
    QString Process::toString() const
    {
    return QString("Type: %1, Date: %2, Number: %3, Price: %4")
    .arg(m_Type).arg(m_Date.toString("dd.MM.yyyy")).ar g(m_NoOfItems).arg(m_PricePerItem);
    }

    Class2:
    QString Items::toString() const
    {
    QString product = QString("Item process: %1\n")
    .arg(Process::toString());

    return process;
    }

    Please assist.

  2. #2
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 42 Times in 41 Posts

    Default Re: Using toString() of another class

    Hello,

    the error message you get is pretty clear: you need to call method toString() with an object of type Process. Your way of calling toString() can only be used if toString() is a static member function of class Process. So your code of class 2 should look like
    Qt Code:
    1. QString Items::toString(const Process& process) const
    2. {
    3. QString product = QString("Item process: %1\n")
    4. .arg(process.toString());
    5.  
    6. return product;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Note that we pass a reference to a Process object to the Items::toString() method and we use this reference for accessing the Process object toString() method.

    One more note: your code would also fail compiling at line
    Qt Code:
    1. return process;
    To copy to clipboard, switch view to plain text mode 

    Best regards
    ars

    P.s.: please use code tags in your post.

Similar Threads

  1. Time is not getting displayed using tostring function!
    By sanujas in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 31st January 2013, 13:15
  2. Custom type registration on QVariant & toString()
    By zickedi in forum Qt Programming
    Replies: 10
    Last Post: 4th March 2012, 00:34
  3. QHostAddress::toString
    By spraff in forum Qt Programming
    Replies: 0
    Last Post: 22nd November 2008, 16:53
  4. Replies: 4
    Last Post: 20th December 2007, 11:51
  5. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 15:36

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
  •  
Qt is a trademark of The Qt Company.