constructor - confused about C++ compiler error -


i'm totally confused. posted code in forum (cplusplus) earlier , got don't understand why compiler gave me error , when asked there there no response hoping explain in more detail here.

this error:

person.cpp: in constructor ‘person::person(qstring)’: person.cpp:12:16: error: no matching function call ‘position::position()’ : m_name(name)

as can see code posted below line pointed in error - person.cpp:12:16 - not "‘position::position()’ : m_name(name).

this suggested solutions (#2 worked):

you didn't specify constructor should used construct m_position compiler assumes want use default constructor (the constructor takes no arguments). problem position doesn't have default constructor why error.

there 2 ways can solve this.

  1. add default position.
  2. specify in person constructor want use existing constructor construct m_position.

person::person(qstring name) : m_name(name), m_position("123", "abc") {

}

this i'm still confused why compiler pointing line in file error; guess, why require constructor @ point? i'm assuming kind of got point stopped , read down file until saw reference position , employer in setposition() balked , printed error.

but if that's case still don't see why need particular constructor , not either of other 2 tried (default no args , cstor 1 arg). when (other forum) point out

"specify constructor should used construct m_position" 

i thought had read somewhere compiler create default copy constructor on fly , expecting happen in setposition() (where position , employer referenced) as, now, i'm trying mechanics of function flesh out little details later.

so that's question: why did compiler that?

here's original code (more or less).

person.cpp ===========================================

// person.cpp // pn mar03-2014  #include <qstring> #include <qtextstream> //#include "position.h" #include "person.h" //#include "employer.h"  person::person(qstring name)   : m_name(name) { }  qstring person::tostring() {   qstring buffer;   qtextstream bufstr(&buffer);   bufstr << "person name: " << m_name << endl;   //buffer = "person name: " + m_name + "\n";   return buffer; }  void person::setposition(employer newc, position newp) {   m_position = newp;   m_employer = newc; } 

person.h =============================================

#ifndef _person_h_  #define _person_h_    #include <qstring> #include "employer.h" #include "position.h"  //class employer; //class position;  class person {    public:      person(qstring name);     void setposition(employer newc, position newp);     qstring tostring();    private:      qstring m_name;     bool m_employed;     position m_position;     employer m_employer;  };  #endif 

position.h ===========================================

#ifndef _position_h_  #define _position_h_    #include <qstring>  //class employer; //class person;  class position {    public:      position(qstring name, qstring description);     qstring tostring();    private:      qstring m_name;     qstring m_description;  };  #endif 

position.cpp =========================================

// position.cpp // pn mar03-2014  #include <qstring> #include <qtextstream> #include "position.h" //#include "person.h" //#include "employer.h"  position::position(qstring name, qstring description)   : m_name(name), m_description(description) {  }  qstring position::tostring() {   qstring buffer;   qtextstream bufstr(&buffer);   bufstr << "position name: " << m_name << endl;   bufstr << "position description: " << m_description << endl;   //buffer = "position name: " + m_name + "\n";   //return buffer.tostdstring();   return buffer; } 

employer.h ===========================================

// employer.h // pn mar08-2014  #ifndef _employer_h_  #define _employer_h_    #include <qstring> //using namespace std;  //class person; //class position;   class employer {    public:      employer(qstring name, qstring market);     // bool hire(person& newhire, position pos);     qstring tostring();    private:      qstring m_name;     qstring m_market;  };  #endif 

employer.cpp ==========================================

// employer.cpp // pn mar05-2014  #include <qstring> #include <qtextstream> #include "position.h" #include "person.h" #include "employer.h"  employer::employer(qstring name, qstring market)   : m_name(name), m_market(market) {}  qstring employer::tostring() {   qstring buffer;   qtextstream bufstr(&buffer);   bufstr << "employer name: " << m_name << endl;   bufstr << "employer market: " << m_market << endl;   //buffer = "person name: " + m_name + "\n";   //return buffer.tostdstring();   return buffer;  //qstring }  /* bool employer::hire(person& newhire, position pos) {   return true;  } */ 

work.cpp (main) ======================================

// work.cpp // pn mar05-2014  #include <qstring> #include <qtextstream> #include <iostream> #include "person.h" #include "employer.h" #include "position.h"  using namespace std;  int main() {    qstring name;   qstring company;   qstring location;   person phil("bozo");   employer sst("sst", "speed");   position posture("standing", "not sitting");   //qtextstream qts();   //qts << phil.tostring();   name = phil.tostring();   cout << name.tostdstring();   company = sst.tostring();   cout << company.tostdstring();   location = posture.tostring();   cout << location.tostdstring();    return 0;  } 

rest of errors =======================================

person.cpp:12:16: note: candidates are: in file included person.h:6:0,                  person.cpp:7: position.h:14:5: note: position::position(qstring, qstring)      position(qstring name, qstring description);      ^ position.h:14:5: note:   candidate expects 2 arguments, 0 provided position.h:9:7: note: position::position(const position&)  class position        ^ position.h:9:7: note:   candidate expects 1 argument, 0 provided person.cpp:12:16: error: no matching function call ‘employer::employer()’    : m_name(name)                 ^ person.cpp:12:16: note: candidates are: in file included person.h:5:0,                  person.cpp:7: employer.h:19:5: note: employer::employer(qstring, qstring)      employer(qstring name, qstring market);      ^ employer.h:19:5: note:   candidate expects 2 arguments, 0 provided employer.h:14:7: note: employer::employer(const employer&)  class employer        ^ employer.h:14:7: note:   candidate expects 1 argument, 0 provided make: *** [person.o] error 1 

======================================================

your class person includes data member m_position of type position

class person {    public:    //...   private:      position m_position;     //... }; 

class position has no default constructor because there explicitly declared constructor parameters

class position {    public:      position(qstring name, qstring description);     //... }; 

when create object of type person constructor

person::person(qstring name)   : m_name(name) { } 

is called. did not initialized data member m_position in mem-initializer list ( initialized data member m_name) constructor tries initialize calling default constructor of class position. pointed out above class position has no default constructor , compiler issues error.

you defined constructor of class person example following way

person::person(qstring name)   : m_name(name), m_position( "", "" ) { } 

or instead of empty strings pass else initialize m_person.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -