Thread: Qt5 / C++ - Trying to set up a class

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    56

    Qt5 / C++ - Trying to set up a class

    Hello,

    I'm tying to convert a function into a class, so I can call it from any cpp module.
    I can get this working,
    formatSDCard(int type, QString diskNumber, bool &result);
    but as soon as I add QProgressBar, I get an error.
    Can somebody help, may be I should use another approach.

    Regards
    Code:
    Header file
    class formatSDCard
    {
    public:
        formatSDCard(int type, QString diskNumber, bool &result, QProgressBar &progress);
    };
    Code:
    cpp file
    formatSDCard::formatSDCard(int type, QString diskNummber, bool &result, QProgressBar &progress)
    {
        progress->setValue(76);
        result = true; //or false;
    }
    Code:
    Calling program
        bool result;
        QString num = "2";
        formatSDCard(1, num, result, ui->progress);
        if (result)
            ...
            ...
        }
    Code:
    Compile error    
    error: no matching function for call to 'formatSDCard::formatSDCard(int, QString&, bool&, QProgressBar*&)'

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I'm tying to convert a function into a class, so I can call it from any cpp module.
    Classes don't do that for you.

    Perhaps you should explain what this means...
    I can get this working,
    formatSDCard(int type, QString diskNumber, bool &result);
    but as soon as I add QProgressBar, I get an error.
    What error?


    Back to your off-beat approach - though I doubt it solves any problems you're facing.
    You need to create an instance of your class, in order to call it's member function.
    Code:
    class formatSDCard myFooFormatter;  //!! this is an instance
    myFooFormatter.formatSDCard(1, num, result, ui->progress);
    This is functionally no different from what you're doing at the moment. The class doesn't add value, just an extra layer of indirection.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello Salem,

    Thanks for your reply.

    When I put in those 2 lines of code, compile errors are:-
    Code:
    class formatSDCard myFooFormatter;  //!! this is an instance
    
    error: no matching function for call to 'formatSDCard::formatSDCard()'
         class formatSDCard myFooFormatter;  //!! this is an instance
                            ^
    Code:
    myFooFormatter.formatSDCard(1, num, result, ui->progress);
    
    error: invalid use of 'formatSDCard::formatSDCard'
         myFooFormatter.formatSDCard(1, num, result, ui->progress);
                        ^
    Regards

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The problem is, your function IS the constructor!

    Perhaps if you gave it a different name (say doFormat), it would work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello Salem,

    Thanks for your reply.

    I changed "formatSDCard" to "doFormat" as suggested.
    Delving more into the compile output I have found these errors as well as the errors previously posted:-
    Code:
    ..\test\noobsform.cpp: In constructor 'noobsForm::noobsForm(const QString&, const QString&, QWidget*)':
    ..\test\noobsform.cpp:22:20: error: no matching function for call to 'doFormat::doFormat()'
         class doFormat myFooFormatter;  //!! this is an instance
                        ^
    In file included from ..\test\noobsform.cpp:12:0:
    ..\test\doFormat.hpp:11:5: note: candidate: doFormat::doFormat(int, QString, bool&, QProgressBar&)
         doFormat(int type, QString diskNumber, bool &result, QProgressBar &progress);
         ^
    ..\test\doFormat.hpp:11:5: note:   candidate expects 4 arguments, 0 provided
    ..\test\doFormat.hpp:7:7: note: candidate: constexpr doFormat::doFormat(const doFormat&)
     class doFormat
           ^
    ..\test\doFormat.hpp:7:7: note:   candidate expects 1 argument, 0 provided
    ..\test\noobsform.cpp:23:20: error: invalid use of 'doFormat::doFormat'
         myFooFormatter.doFormat(1, num, result, ui->progress);
                        ^
    Regards

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I didn't say change ALL the names!

    Code:
    // .h file
    class formatSDCard
    {
    public:
        formatSDCard(); //!! this is the CONSTRUCTOR (if you need one - remove it if you don't)
        doFormat(int type, QString diskNumber, bool &result, QProgressBar &progress);
    };
    
    // .cpp file
    formatSDCard::doFormat(int type, QString diskNummber, bool &result, QProgressBar &progress)
    {
        progress->setValue(76);
        result = true; //or false;
    }
    
    
    // user
    formatSDCard myFooFormatter;  //!! this is an instance
    myFooFormatter.doFormat(1, num, result, ui->progress);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello Salem,

    Sorry, my mistake.
    I didn't say change ALL the names!
    Code:
    #ifndef FORMATSD_HPP
    #define FORMATSD_HPP
    
    class formatSDCard
    {
    public:
        //formatSDCard();
        doFormat(int type, QString diskNumber, bool &result); //, QProgressBar &progress);
    };
    
    #endif // FORMATSD_HPP
    Code:
    #include <QString>
    #include "formatSD.hpp"
    
    formatSDCard::doFormat(int type, QString diskNummber, bool &result) //, QProgressBar &progress)
    {
        //progress->setValue(76);
        result = true; //or false;
    }
    Code:
        bool result;
    
        formatSDCard myFooFormatter;  //!! this is an instance
        myFooFormatter.doFormat(1, num, result); //, ui->progress);
        qDebug() << "result" << result;
        
    output:-
    result true
    Code:
    Compiler warning:-
    ..\test\formatSD.cpp: In member function 'int formatSDCard::doFormat(int, QString, bool&)':
    ..\test\formatSD.cpp:14:1: warning: no return statement in function returning non-void [-Wreturn-type]
     }
     ^
    This works without QProgressBar, apart from a warning.
    I will post later the output when including the QProgressBar.

    Regards

  8. #8
    Registered User
    Join Date
    May 2015
    Posts
    56
    Hello Salem,

    Thanks for your patience and help.
    I hope I've got it right.
    Code:
        formatSDCard myFooFormatter; // !! this is an instance
        myFooFormatter.doFormat(1, num, result, ui->progress);
        
    error: no matching function for call to 'formatSDCard::doFormat(int, QString&, bool&, QProgressBar*&)'
        myFooFormatter.doFormat(1, num, result, ui->progress);
                                                            ^
    Code:
        formatSDCard myFooFormatter; // !! this is an instance
        myFooFormatter.doFormat(1, num, result, *ui->progress);
        
    This compiles fine, adding the pointer.
    Code:
    Just the warning:-
    ..\test\formatSD.cpp: In member function 'int formatSDCard::doFormat(int, QString, bool&, QProgressBar&)':
    ..\test\formatSD.cpp:14:1: warning: no return statement in function returning non-void [-Wreturn-type]
     }
     ^
    edit: OK, got rid of the warning by adding return(0); to the class .cpp.

    Regards
    Last edited by mad-hatter; 09-09-2016 at 05:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-18-2012, 11:17 AM
  2. Replies: 7
    Last Post: 05-26-2005, 10:48 AM
  3. Replies: 2
    Last Post: 04-06-2005, 07:25 AM
  4. Template <class T1, class T2, class T3> error LNK2019
    By JonAntoine in forum C++ Programming
    Replies: 9
    Last Post: 10-11-2004, 12:25 PM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM

Tags for this Thread