【发布时间】: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"。