Home > Enterprise >  how to call overloaded function in the same version of another in c
how to call overloaded function in the same version of another in c

Time:01-22

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.

CodePudding user response:

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 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

CodePudding user response:

problem was call by reference.

converting QString& to const QString& gets the job done. issue is, we may not be able to have string& a = b and then string& c = a to achive this. we must use pointers or constant type.

that is of course accourding to my understanding and seems to work

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

};

inline QString A::insert(const QString &id, const QString &name, const QString &email_id, const QString &contact_id, const QString &reg_num, Address addr, const QUrl &urlToLocalFile) {
    return this->insert(id,name,email_id,contact_id,reg_num,addr,urlToLocalFile.toLocalFile());
}
inline QString A::insert(const QString &id, const QString &name, const QString &email_id, const QString &contact_id, const QString &reg_num, Address addr, const QString &pathToLocalFile) {
}
  •  Tags:  
  • Related