compiler error quotes wrong line?

Now I'm totally confused. I posted this code in another topic earlier and got help with that problem but now I have another that seems even stranger to me. This is the first error (for reference the rest of the errors are after the code:

1
2
3
4
  person.cpp: In constructor ‘Person::Person(QString)’:
person.cpp:12:16: error: no matching function for call to ‘Position::Position()’
   : m_Name(name)


Here's the code. As you can see the line pointed to in the error - person.cpp:12:16 - is not "‘Position::Position()’
: m_Name(name).

person.cpp ===========================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 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 =============================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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 ===========================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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 =========================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 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 ===========================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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 


emplyer.cpp ==========================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 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) ======================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 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 =======================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
person.cpp:12:16: note: candidates are:
In file included from person.h:6:0,
                 from 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 for call to ‘Employer::Employer()’
   : m_Name(name)
                ^
person.cpp:12:16: note: candidates are:
In file included from person.h:5:0,
                 from 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


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

TIA for any pointers.
You didn't specify what constructor should be used to construct m_Position so the compiler assumes you want to use the default constructor (the constructor that takes no arguments). The problem is that Position doesn't have a default constructor so that is why you get the error.

There are two ways you can solve this.
1. Add a default to Position.
2. Specify in the Person constructor that you want to use an existing constructor to construct m_Position.
1
2
3
4
5
6
Person::Person(QString name)
  : m_Name(name),
    m_Position("123", "abc")
{
  
}

Peter87 Thanks

I tried this as a default in position.cpp:

Position::Position() {}

but that didn't work so I tried:

Position::Position(QString name):m_Name(name){}

but that didn't and I had to remove both of them, so then I tried:

1
2
3
Person::Person(QString name)
  : m_Name(name), m_Position("default","strange"), m_Employer("me","you")
{}


in person.cpp but had to remove

1
2
3
Person::Person(QString name)
  : m_Name(name)
{}


to get it to work.

If you have a minute, I'm still confused about why the compiler would be pointing to that line in that file with that error; I guess, why it would require that constructor at that point. I'm assuming it kind of got to that point then stopped and read down the file until it saw the reference to Position and Employer in setPosition() then balked and printed the error.

But I still don't see why it would need that particular constructor and not either of the other two I tried. When you point out
specify what constructor should be used to construct m_Position
I thought I had read somewhere that the compiler would create a default copy constructor on the fly and that is what I was expecting would happen in setPosition() (where Position and Employer are referenced) as, for now, I'm just trying to get the mechanics of this to function then I would flesh out the little details later.
Topic archived. No new replies allowed.