对C ++编译器错误感到困惑

时间:2014-03-10 02:23:48

标签: c++ constructor compiler-errors

我完全糊涂了。我之前在另一个论坛(cplusplus)发布了这个代码并得到了帮助,但我不明白为什么编译器给了我这个错误,当我问到那里没有回复所以我希望有人可以更详细地解释它这里。

这是错误:

  

person.cpp:在构造函数'Person :: Person(QString)'中:   person.cpp:12:16:错误:没有匹配函数来调用'Position :: Position()'    :m_Name(name)

从下面发布的代码中可以看出错误中指向的行 - person.cpp:12:16 - 不是“'Position :: Position()'    :m_Name(name)。

这是建议的解决方案(#2工作):

  

您没有指定应该使用什么构造函数来构造m_Position,因此编译器假定您要使用默认构造函数(不带参数的构造函数)。问题是Position没有默认构造函数,这就是你得到错误的原因。

     

有两种方法可以解决这个问题。

     
      
  1. 将默认值添加到位置。
  2.   
  3. 在Person构造函数中指定要使用现有构造函数构造m_Position。
  4.         

    Person :: Person(QString名称)    :m_Name(name),      m_Position(“123”,“abc”)   {

         

    }

这是我仍然困惑的地方关于为什么编译器会指向该文件中该行的那个错误;我想,为什么在那一点需要构造函数?我假设有点到达那一点然后停止并读取文件,直到它在setPosition()中看到对Position和Employer的引用然后拒绝并打印错误。

但是如果是这种情况那么我仍然不明白为什么它需要那个特定的构造函数而不是我尝试的其他两个(默认没有args和cstor和一个arg)。当你(其他论坛)指出

"specify what constructor should be used to construct m_Position"

我以为我已经阅读过编译器会动态创建默认复制构造函数的地方,这就是我期望在setPosition()(引用Position和Employer)中发生的事情,因为,现在,我是只是试图让这个机制起作用然后我会在以后充实细节。

这就是我的问题:为什么编译器会这样做?

这是原始代码(或多或少)。

person.cpp =========================================== < / p>

// 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 =========================================== < / p>

#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 =========================================== < / p>

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

}

其他错误=======================================

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

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

2 个答案:

答案 0 :(得分:1)

您的类Person包含Position

类型的数据成员m_Position
class Person
{

  public:

  //...
  private:

    Position m_Position;
    //...
};

类位置没有默认构造函数,因为显式声明了带参数的构造函数

class Position
{

  public:

    Position(QString name, QString description);
    //...
};

当你创建Person类型的对象时,它的构造函数

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

被调用。由于您没有在mem-initializer列表中初始化其数据成员m_Position(您只初始化了数据成员m_Name),因此构造函数尝试初始化它本身,调用类Position的默认构造函数。但是正如我在上面指出的那样,Position没有默认构造函数,编译器会发出错误。

您可以定义Person类的构造函数,例如以下方式

Person::Person(QString name)
  : m_Name(name), m_Position( "", "" )
{
}

或者代替空字符串,您可以传递其他内容来初始化m_Person。

答案 1 :(得分:1)

简短的回答:你有一个循环的依赖。并且您尝试将字符串转换为QString。

答案很长: 雇主.cpp#包括这个人,但person.h是#including雇主。 永远不应该有循环依赖。

这是一张图片来说明: http://i.stack.imgur.com/AdZ8i.jpg

此外,您正在尝试将字符串(或c字符串)转换为QString。尝试创建辅助函数以简化转换(如果还没有创建)。

最后,您需要初始化所有成员变量。

Person::Person(QString name)
: m_Name(name),
m_Employed(false),
m_Position(Position(QString(), QString())),
m_Employer(Employer(QString(), QString()))
{
}
相关问题