简体   繁体   中英

Why am I getting a "no matching function" error?

Pretty new to C++, I was given an assignment that is basically about utilizing two different classes, however when creating my header files and c++ files and attempting to compile I get an error that reads no matching function for call to 'Owner::Owner()' . Since I am not super familiar with C++ yet I am assuming that this issue has something to do with my constructors and the way I am trying to call them, my assignment details what I think my issue is about but I am having trouble understanding exactly what needs to be done. I will provide the assignment details on the issue, as well as the code and compilation error below. Sorry for all the information I've just been stuck with this issue for a while and I can't seem to figure out a solution.

编译错误图片

Transcribed Error In constructor 'Dog::Dog(std::__cxx11::string, int)': Dog.cpp:23:46: error: no matching function for call to 'Owner::Owner()' Dog::Dog(std::string unsetBreed, int unsetAge){

Assignment Details

Now you will write a program that consists of two classes, a Dog class and an Owner class. Their specification is shown in the UML diagram below. Notice that in our design, every Dog has an Owner class member. Class Owner is immutable, as mentioned above. An immutable class is just a class whose members cannot be changed(mutated) after an object was instantiated. Therefore, the Owner class does not have any setter methods. The Owner's class attributes must be set at the time of creation(in the Owner's constructor). You will call Owner's constructor from inside Dog's constructor. Do not forget to do it inside each constructor in class Dog.

UML图

Dog.h File

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

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

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

#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();
}

The problem here is that you don't have a constructor that receives 0 parameters. You have 2 options:

1 - Define other constructor:

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

2 - Define default params:

//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();
}
...

Note: I don't know why you are calling getter in the constructors.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM