Results 1 to 6 of 6

Thread: How to use a class multiple times?

  1. #1
    Join Date
    Jul 2013
    Posts
    36
    Thanks
    14
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5

    Question How to use a class multiple times?

    I need a little syntax help please

    Qt Code:
    1. some_class some_function(QString some_string) {
    2. some_class string_collection;
    3. if (some_string.isEmpty()) {
    4.  
    5. more_strings = new some_class("Warning", "Error", "FooBar");
    6. }
    7. else {
    8.  
    9. more_strings = new some_class("Warning1", "Error1", "FooBar1");
    10. }
    11.  
    12. return more_strings; }
    To copy to clipboard, switch view to plain text mode 
    I can't compile that ... where is my mistake?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use a class multiple times?

    Crystal ball does not work. Show the real code and messages from the compiler.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to use a class multiple times?

    Qt Code:
    1. some_class * some_function(QString some_string) {
    2. //some_class string_collection;
    3. some_class * more_strings = 0;
    4. if (some_string.isEmpty()) {
    5.  
    6. more_strings = new some_class("Warning", "Error", "FooBar");
    7. }
    8. else {
    9.  
    10. more_strings = new some_class("Warning1", "Error1", "FooBar1");
    11. }
    12. return more_strings; }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #4
    Join Date
    Jul 2013
    Posts
    36
    Thanks
    14
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5

    Default Re: How to use a class multiple times?

    @Santosh Reddy:
    I am a little bit confused with
    Qt Code:
    1. some_class * some_function(QString some_string)
    To copy to clipboard, switch view to plain text mode 
    in your example.

    @Lesilok: The whole code I had in the beginning. Compiler says: "Fehler:C2664: 'MultiReturn::MultiReturn(const MultiReturn &)': converting parameter 1 from 'MultiReturn *' to 'const MultiReturn &' not possible"
    Qt Code:
    1. MultiReturn read_in(QString incoming_string) {
    2.  
    3. MultiReturn string_collection;
    4. if (incoming_string.isEmpty()) {
    5. string_collection = new MultiReturn("Warning", "Error", "Mind");
    6. }
    7. else {
    8. string_collection = new MultiReturn("Warning1", "Error1", "Mind1");
    9. }
    10. return string_collection; }
    To copy to clipboard, switch view to plain text mode 
    When I do it like this, it compiles, but crashes instantly (thats what I made from Santosh Reddy's advice):
    Qt Code:
    1. MultiReturn read_in(QString incoming_string) {
    2.  
    3. MultiReturn *string_collection = 0;
    4. if (incoming_string.isEmpty()) {
    5. string_collection = new MultiReturn("Warning", "Error", "Mind");
    6. }
    7. else {
    8. string_collection = new MultiReturn("Warning1", "Error1", "Mind1");
    9. }
    10. return *string_collection; }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: How to use a class multiple times?

    Discard the "new" keyword from your original code.
    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.


  6. The following user says thank you to wysota for this useful post:

    xtlc (8th August 2013)

  7. #6
    Join Date
    Jul 2013
    Posts
    36
    Thanks
    14
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5

    Default Re: How to use a class multiple times?

    Thanks wysota, that did the trick!

Similar Threads

  1. Replies: 8
    Last Post: 28th March 2013, 01:12
  2. Hiding and showing the same mainwindow multiple times
    By Charvi in forum Qt Programming
    Replies: 2
    Last Post: 20th June 2012, 12:19
  3. Replies: 1
    Last Post: 3rd January 2012, 10:25
  4. Can I call setRange multiple times for QProgressBar ?
    By elizabeth.h1 in forum Qt Programming
    Replies: 2
    Last Post: 30th April 2010, 07:18
  5. Contex menu appear multiple times
    By kubas in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2009, 14:55

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.