【问题标题】:Why am I getting a "no matching function" error?为什么我会收到“无匹配功能”错误?
【发布时间】:2020-02-20 02:54:42
【问题描述】:

对 C++ 很陌生,我得到的任务基本上是关于利用两个不同的类,但是在创建我的头文件和 c++ 文件并尝试编译时,我收到一个错误,显示 no matching function for call to '所有者::所有者()'。由于我对 C++ 不是非常熟悉,但我假设这个问题与我的构造函数以及我尝试调用它们的方式有关,我的分配详细说明了我认为我的问题是什么,但我无法准确理解是什么需要完成。我将提供有关该问题的分配详细信息,以及下面的代码和编译错误。抱歉,我刚刚被这个问题困扰了一段时间,我似乎无法找到解决方案。

转录错误 在构造函数 'Dog::Dog(std::__cxx11::string, int)' 中: Dog.cpp:23:46: 错误: 没有匹配函数调用'Owner::Owner()' Dog::Dog(std::string unsetBreed, int unsetAge){

作业详情

现在您将编写一个由两个类组成的程序,一个 Dog 类和一个 Owner 类。它们的规范显示在下面的 UML 图中。请注意,在我们的设计中,每个 Dog 都有一个 Owner 类成员。如上所述,类所有者是不可变的。不可变类只是在实例化对象后其成员无法更改(变异)的类。因此,Owner 类没有任何 setter 方法。 Owner 的类属性必须在创建时设置(在 Owner 的构造函数中)。您将从 Dog 的构造函数中调用 Owner 的构造函数。不要忘记在 Dog 类的每个构造函数中执行此操作。

Dog.h 文件

#ifndef DOG_H_INCLUDED
#define DOG_H_INCLUDED

#include <iostream>
#include "Owner.h"


class Dog {
//-----------------------//
  private:
    std::string breed;
    int age;
    Owner owner;
    static int dogCount;
//-----------------------//
  public:
    Dog();
    Dog(std::string, int);
    std::string getBreed();
    int getAge();
    void setBreed(std::string);
    void setAge(int);
    void printDogInfo();
    int getDogCount();
};
#endif // DOG_H_INCLUDED

Owner.h 文件

#ifndef OWNER_H_INCLUDED
#define OWNER_H_INCLUDED
#include <iostream>

class Owner {
//-----------------------//
  private:
    std::string name;
    int age;
//-----------------------//
  public:
    Owner(std::string, int);
    std::string getName();
    int getAge();
//-----------------------//
};


#endif // OWNER_H_INCLUDED

Dog.cpp 文件

#include <iostream>
#include "Owner.cpp"
#include "Owner.h"
#include "Dog.h"

//---------------SETTERS------------------//
void Dog::setBreed(std::string dogBreed){dogBreed = breed;}
void Dog::setAge(int dogAge){dogAge = age;}





//--------------GETTERS------------------//
std::string Dog::getBreed(){return breed;}
int Dog::getAge(){return age;}
int Dog::getDogCount(){return dogCount;}




//--------------OTHERS-------------------//
Dog::Dog(std::string unsetBreed, int unsetAge){
  Owner::Owner(std::string unsetName, int unsetOwnerAge);
  Dog::setBreed(unsetBreed);
  Dog::setAge(unsetAge);
}

void Dog::printDogInfo(){
  Dog::getBreed();
  Dog::getAge();
}

Owner.cpp 文件

#include <iostream>
#include "Owner.h"
#include "Dog.h"

//--------------GETTERS------------------//
int Owner::getAge(){return age;}
std::string Owner::getName(){return name;}


//--------------OTHERS-------------------//
Owner::Owner(std::string unsetName, int unsetOwnerAge){
  Owner::getName();
  Owner::getAge();
}

【问题讨论】:

  • 先从 Dog.cpp 文件中取出include "Owner.cpp"

标签: c++ class object


【解决方案1】:

这里的问题是你没有接收0个参数的构造函数。 你有两个选择:

1 - 定义其他构造函数:

//header file
class Owner {
...
  public:
    Owner(std::string, int);
    Owner();
...
};
//cpp file
...
Owner::Owner(){
  name = "Jhon Doe";
  age = 18;
}
...

2 - 定义默认参数:

//header file
class Owner {
...
  public:
    Owner(std::string unsetName = "John Doe", int unsetAge = 18);
...
};
//cpp file
...
Owner::Owner(std::string unsetName = "John Doe", int unsetAge = 18);
  Owner::getName();
  Owner::getAge();
}
...

注意:我不知道你为什么在构造函数中调用 getter。

【讨论】:

  • 3.使用初始化列表初始化owner
【解决方案2】:

看起来您的 Owner 类没有默认构造函数尝试包括 Owner::Owner() 构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-22
    • 2017-01-04
    • 1970-01-01
    • 2022-06-29
    • 2019-03-18
    • 1970-01-01
    • 2017-11-30
    • 2013-03-09
    相关资源
    最近更新 更多