Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 argument
}; //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()); 
}

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 semicolor ; 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 functions should be const QString& pathToLocalFile instead of QString& pathToLocalFile

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading