gpt4 book ai didi

c++ - 没有匹配函数调用 'class'

转载 作者:行者123 更新时间:2023-11-27 22:42:50 24 4
gpt4 key购买 nike

我正在努力学习和理解多重继承。

我有一个“squat”类,它是“abandoned”类的 child

为什么我会收到错误消息 no matching function for call to Abandoned::Abandoned()

squatter.cpp|9|错误:没有匹配函数来调用'Abandoned::Abandoned()'|

我在这个网站上看了很多与这个主题相关的帖子。他们说我必须用正确的参数声明一个 Abandoned 对象,但我并没有尝试使用 Abandoned 中的任何函数,我现在只想将它链接起来。

我认为这与我的构造函数有关,但我无法弄清楚问题出在哪里。

抱歉代码太长,但我想不出更好的方法

这是我的深蹲.cpp/h

#ifndef SQUAT_H
#define SQUAT_H
#include<abandoned.h>
#include<building.h>

class Squat:public Buildings, public Abandoned
{
private:
bool isempty;

public:

Squat(bool);
virtual void display();
virtual void isoccupied(bool);


};



#endif // SQUAT_H

和.cpp

#include<iostream>
#include<squat.h>
#include<building.h>
#include<apartment.h>
#include<abandoned.h>


Squat::Squat(bool isitempty):isempty(isitempty){}

void Squat::isoccupied(bool isitempty)
{
if(isitempty=1)
{
isempty=1;
cout<<"The abandoned building is empty"<<endl;
}
else cout<<"The abandoned building is full of dirty squatters"<<endl;
}

我的理解是当我说的时候;

class Squat:public Buildings, public Abandoned

这将类“链接”在一起

下面是我废弃的.cpp/h

#ifndef ABANDONED_H
#include<vector>
#include<building.h>
#define ABANDONED_H


class Abandoned:public Buildings
{

private:
int length;
std::vector<int> status;
int sum;

public:

Abandoned(int m_size, int asum);
Abandoned(bool);
virtual void getstatus(int);
virtual void display();
virtual void demolish(int);
virtual void rebuild(int);
//virtual void demolish(int);



};






#endif // ABANDONED_H

和我的.cpp

#include<iostream>
#include<string>
#include<vector>
#include<apartment.h>
#include<building.h>
#include<abandoned.h>
#include<algorithm>
#include<numeric>
using namespace std;

Abandoned::Abandoned(int m_size, int asum): length(m_size), status(m_size, 0), sum(asum)
{}


void Abandoned::getstatus(int m_size)
{
status.push_back(length);

};

void Abandoned::display()
{
Buildings::display();
cout << " length of status is: "<<status.size()<<endl;
}

void Abandoned::demolish(int asum)
{
if(asum<3)
{
cout<<"The building is below the safety standards and should be demolished"<<endl;
}else{
cout<<"The building meets the safety standards and can be rebuilt"<<endl;}

}

void Abandoned::rebuild(int asum)
{
if(asum>3)
{
cout<<"The building is above the safety standards and should be rebuilt"<<endl;
}else{
cout<<"The building should be demolished"<<endl;}

}

最佳答案

Abandoned 类没有无参构造函数。这意味着派生类的任何构造函数都必须从 Abandoned 中选择一个构造函数来调用:

Squat::Squat(bool isitempty): Abandoned(...), isempty(isitempty){}

关于c++ - 没有匹配函数调用 'class',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46976160/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com