Results 1 to 7 of 7

Thread: Int to String - manipulating string

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Int to String - manipulating string

    Hello, I have to wirte this method and I'd like one efficent and compact:
    Qt Code:
    1. int _low, _high;
    2. std::string MyClass:: print() {
    3. std::string str;
    4. std::string result;
    5. std::stringstream ss;
    6.  
    7. result += "[";
    8. ss << _low;
    9. ss >> str;
    10. result += str;
    11.  
    12. result.append("-");
    13.  
    14. ss << _high;
    15. ss >> str;
    16. result += str;
    17. result.append("]");
    18. return result;
    19. }
    20. //main
    21. MyClass mycl;
    22. cout << " The range is: " << mycl.print() << endl
    To copy to clipboard, switch view to plain text mode 
    it must print: "the range is [10-20]"

    It seems too large and not compact.... How can I improve?
    Thanks.
    Regards

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Int to String - manipulating string

    Implement an output stream operator for your class like this:
    Qt Code:
    1. inline std::ostream& operator<<(std::ostream& os, const MyClass& c)
    2. { return os << "[" << c.low() << "-" << c.high() << "]"; }
    To copy to clipboard, switch view to plain text mode 

    In action:
    Qt Code:
    1. MyClass mycl;
    2. cout << " The range is: " << mycl << endl;
    To copy to clipboard, switch view to plain text mode 
    Last edited by DeepDiver; 3rd November 2007 at 21:53. Reason: updated contents

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Int to String - manipulating string

    hello, thanks but I had an error like this:
    error C2804: binary 'operator <<' has too many parameters
    Regards

  4. #4
    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: Int to String - manipulating string

    Did you implement it as a method of your class or as a standalone function? It should be the latter.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Int to String - manipulating string

    Quote Originally Posted by wysota View Post
    Did you implement it as a method of your class or as a standalone function? It should be the latter.
    I had to declare it as friend method.....because my situation was it:
    Qt Code:
    1. //subnode.cpp
    2. class SubNode {
    3. protected:
    4. int _low, _high;
    5. }
    6.  
    7. //node.cpp
    8. class Node : SubNode {
    9. ........
    10. int other;
    11.  
    12. };
    13. friend inline std::ostream& operator<<(std::ostream& os, const Node& n) {
    14. return os << "[" << n._low << "-" << n._high << "]";
    15. }
    16. //end node.cpp
    To copy to clipboard, switch view to plain text mode 
    If I declare the inline function in node.cpp but after class declaration ( after "};" ) it gets an error that it can't acces to protected member.
    Is it ok in this way or I can be better?
    Regards

  6. #6
    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: Int to String - manipulating string

    It has to be a standalone function, not a method. If you want it to be a method of your class, you have to omit the second parameter (const Node& n).

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Int to String - manipulating string

    Hi, I declared it as below. it seem works both them
    Qt Code:
    1. //subnode.cpp
    2. class SubNode {
    3. protected:
    4. int _low, _high;
    5. }
    6.  
    7. //node.cpp
    8. class Node : SubNode {
    9. ........
    10. int other;
    11. friend std::ostream& operator<<(std::ostream& os, const Node& n) {
    12. return os << "[" << n._low << "-" << n._high << "]";
    13. }
    14. };
    15. //inline std::ostream& operator<<(std::ostream& os, /*const*/ Node& n) {
    16. // return os << "[" << n.GETLOW() << "-" << n.GETHIGH() << "]";
    17. // }
    18. //end node.cpp
    To copy to clipboard, switch view to plain text mode 
    Out of class 'const' gets error; idem for _low and _high that I coudn't access them because they are protected....
    Regards

Similar Threads

  1. Reading from sockets in a multithreaded program
    By KoosKoets in forum Qt Programming
    Replies: 9
    Last Post: 4th April 2007, 20:43
  2. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40

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.