[SOLVED] how to call overloaded function in the same version of another in c++

Issue

Greetings I have a class with overloaded functions I do not want to re type whole thing again and again so I created function(s) with different parameters see below example and I get compilation errors please help consider below snippet

class A {
public:
    inline A();
    inline ~A();    
    QString insert(QString& id, QString& name, QString& email_id, QString& contact_id, QString& reg_num, Address addr, QUrl& urlToLocalFile );
    QString insert(QString& id, QString& name, QString& email_id, QString& contact_id, QString& reg_num, Address addr, QString& pathToLocalFile );
}

inline QString insert(QString& id, QString& name, QString& email_id, QString& contact_id, QString& reg_num, Address addr, QUrl& urlToLocalFile ) {
    this->insert(id, name, email_id, contact_id, reg_num, addr, urlToLocalFile.toLocalFile()); // throws error no matching member function for call to insert
}

inline QString insert(QString& id, QString& name, QString& email_id, QString& contact_id, QString& reg_num, Address addr, QString& pathToLocalFile ) {   
    /*some tasks*/ 
}

Thanks in advance.

Solution

You have to be in the scope of the class A when defining the member functions outside the class. This can be done using A:: as shown below.

class A {
public:
    inline A();
    inline ~A();    
    QString insert(QString& id, QString& name, QString& email_id, QString& contact_id, QString& reg_num, Address addr, QUrl& urlToLocalFile );
    QString insert(QString& id, QString& name, QString& email_id, QString& contact_id, QString& reg_num, Address addr, const QString& pathToLocalFile ); //note the const added for the last parameter
}; //added missing semicolon here

//note the A:: used below
inline QString A::insert(QString& id, QString& name, QString& email_id, QString& contact_id, QString& reg_num, Address addr, QUrl& urlToLocalFile ) {
    this->insert(id, name, email_id, contact_id, reg_num, addr, urlToLocalFile.toLocalFile()); 
}
//note the const added in the last parameter
inline QString A::insert(QString& id, QString& name, QString& email_id, QString& contact_id, QString& reg_num, Address addr, const QString& pathToLocalFile ) { 
    /*some tasks*/ 
}

Additionally you’re missing a semicolon ; after class definition.

Also, note that we cannot bind non-const lvalue reference to an rvalue. So the last parameter of the second insert member function should be const QString& pathToLocalFile instead of QString& pathToLocalFile

Answered By – Anoop Rana

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *