类 和构造函数有很多错误和问题
A lot of errors and problems with classes and constructors
这是文件“lib.h”
的代码
/**
* @file lib.h
* @brief Файл с определением функций для решения заданий из лабораторной работы №23(unready)
*
* @author Taradai S.V.
* @version 0.0
* @date 19-май-2021
*/
#include <iostream>
#include <string>
#include <cstdio>
struct BirdHome{
int HomeArea;
int HomeHeight;
int HomeFeederQuantity;
bool HomeHasNest;
BirdHome();
BirdHome(int homearea, int homeheight, int feederquant, bool nest);
};
class Bird{
private:
bool IsRinged;
std::string Specie;
int BirdAge;
std::string Gender;
struct BirdHome home;
public:
Bird();
Bird(const Bird& copy);
Bird(bool ring,std::string spec, int age,std::string gend, struct BirdHome hinfo);
~Bird();
void set_IsRinged(const bool ring);
void set_Specie(const std::string spec);
void set_BirdAge(const int age);
void set_Gender(const std::string gend);
void set_BirdHome(const struct BirdHome hinfo);
bool get_IsRinged()const;
std::string get_Specie()const;
int get_BirdAge()const;
std::string get_Gender()const;
struct BirdHome get_BirdHome()const;
};
class Array{
private:
Bird** arr;
int size;
int maxsize;
public:
Array();
~Array();
Bird& getBird(int index)const;
void Showall() const;
void ShowBird(Bird bird)const;
void addBird(const Bird& bird);
void removeBird(int index)const;
};
这是文件“lib.cpp”
的代码
/**
* @file lib.c
* @brief Файл с реализацией функций для решения заданий из лабораторной работы №23(unready)
*
* @author Taradai S.V.
* @version 0.0
* @date 19-маЙ-2021
*/
#include "lib.h"
BirdHome::BirdHome{
}
BirdHome::BirdHome(int homearea, int homeheight, int feederquant, bool nest)
: HomeArea(homearea),
HomeHeight(homeheight),
HomeFeederQuantity(feederquant),
HomeHasNest(nest){
}
Bird::Bird(){
}
Bird::Bird(const Bird& copy)
: IsRinged(copy.IsRinged),
Specie(copy.Specie),
BirdAge(copy.BirdAge),
Gender(copy.Gender),
home(copy.home.HomeArea,copy.home.HomeHeight,copy.home.HomeFeederQuantity,copy.home.HomeHasNest){
}
Bird::Bird(bool ring,std::string spec, int age,std::string gend, struct BirdHome hinfo)
:IsRinged(ring),
Specie(spec),
BirdAge(age),
Gender(gend),
home(hinfo){
}
Bird::~Bird(){
}
void Bird::set_IsRinged(const bool ring){
IsRinged=ring;
}
void Bird::set_Specie(const std::string spec){
Specie=spec;
}
void Bird::set_BirdAge(const int age){
BirdAge=age;
}
void Bird::set_Gender(const std::string gend){
Gender=gend;
}
void Bird::set_BirdHome(const struct BirdHome hinfo){
home.HomeArea=hinfo.HomeArea;
home.HomeHeight=hinfo.HomeHeight;
home.HomeFeederQuantity=hinfo.HomeFeederQuantity;
home.HomeHasNest=hinfo.HomeHasNest;
}
bool Bird::get_IsRinged()const{
return IsRinged;
}
std::string Bird::get_Specie()const{
return Specie;
}
int Bird::get_BirdAge()const{
return BirdAge;
}
std::string Bird::get_Gender()const{
return Gender;
}
struct BirdHome Bird::get_BirdHome()const{
return home;
}
Array::Array():size(0){
birds=new Bird*[maxsize];
}
Array::~Array(){
for(int i=0;i<maxsize;i++){
delete birds[i];
}
delete[] birds;
}
Bird& Array::getBird(int index)const{
return *birds[index];
}
void Array::Showall() const{
for(int i=0;i<size;i++){
printf("\nBirds %d",i+1);
ShowBird(getBird(i));
}
}
void Array::ShowBird(Bird bird)const{
printf("\n\tIs the birds ringed:%s\n\tThe bird's name specie:%s\n\tThe bird's age is:%d\n\tThe birdd's gender is:%s\n\tThe bird's home area(in square cm) is:%d\n\tThe bird's home height(in cm) is:%d\n\tAmount of feeders in bird's home is:%d\n\tDoes the bird's home has nest:%s",
bird.get_IsRinged()?"true":"false",
bird.get_Specie().c_str(),
bird.get_BirdAge(),
bird.get_Gender().c_str(),
bird.get_BirdHome().HomeArea,
bird.get_BirdHome().HomeHeight,
bird.get_BirdHome().HomeFeederQuantity,
bird.get_BirdHome().HomeHasNest?"true":"false");
}
void Array::addBird(const Bird& bird){
if(size<maxsize){
birds[size]=new Bird(bird);
size++;
}else{
printf("\nThe limit is exceeded");
}
}
void Array::removeBird(int index)const{
delete birds[index];
while (index<size){
birds[index]=birds[index+1];
index++;
}
size--;
birds[index]=nullptr;
}
这是文件“main.cpp”
的代码
/**
* @file main.c
* @brief Файл с демонстрацией решения заданий из лабораторной работы №23(unready)
*
* @author Taradai S.V.
* @version 0.0
* @date 19-май-2021
*/
#include "lib.cpp"
int main(){
Array birds;
birds.addBird(Bird(true,"False",5,"Male",BirdHome(100,50,3,false)));
birds.addBird(Bird(false,"Crane",12,"Female",BirdHome(100,100,1,true)));
birds.addBird(Bird(false,"Griffin",36,"Male",BirdHome(500,200,10,true)));
birds.Showall();
printf("\nThat is what we've found at the index 2:");
birds.ShowBird(birds.getBird(2));
printf("\nNow we'll delete the bird at the index 1");
birds.removeBird(1);
printf("This is what we got from it:");
birds.Showall();
return 0;
}
这是文件“Makefile”的代码
all:clean prep compile run format
clean:
rm -rf dist
prep:
mkdir dist
compile:main.bin test.bin
main.bin:
clang++ -g -I./src ./src/main.cpp -o ./dist/main.bin
test.bin:
clang++ -g ./test/test.cpp -o ./dist/test.bin
format:
doxygen Doxyfile
好吧,这个程序应该按以下方式工作:我们有一个 class“Bird”元素的动态数组,其中一个元素有 4 个特征,还有一个包含 4 个特征的结构“BirdHome”当前元素的特征。当 运行 这个程序时,你会自动用一个带参数的构造函数初始化数组中的三个元素(我还有一个默认的构造函数和一个复制的构造函数,但现在不需要它们了)。然后它打印所有这些,然后向您显示具有特定索引的元素,然后删除具有特定索引的元素。然后你会看到这个数组的内容。但是我遇到了 13 个错误,我真的不知道如何修复它们。我只是试图重写某人的代码,但使用我的元素,但出现了这个错误:
In file included from ./src/main.cpp:9:
./src/lib.cpp:11:11: error: qualified reference to 'BirdHome' is a constructor name rather than a type in this context
BirdHome::BirdHome{
^
./src/lib.cpp:11:19: error: expected unqualified-id
BirdHome::BirdHome{
^
./src/lib.cpp:92:3: error: use of undeclared identifier 'birds'
birds=new Bird*[maxsize];
^
./src/lib.cpp:97:12: error: use of undeclared identifier 'birds'
delete birds[i];
^
./src/lib.cpp:99:12: error: use of undeclared identifier 'birds'
delete[] birds;
^
./src/lib.cpp:103:11: error: use of undeclared identifier 'birds'
return *birds[index];
^
./src/lib.cpp:127:5: error: use of undeclared identifier 'birds'; did you mean 'bird'?
birds[size]=new Bird(bird);
^~~~~
bird
./src/lib.cpp:125:33: note: 'bird' declared here
void Array::addBird(const Bird& bird){
^
./src/lib.cpp:127:10: error: type 'const Bird' does not provide a subscript operator
birds[size]=new Bird(bird);
~~~~~^~~~~
./src/lib.cpp:135:10: error: use of undeclared identifier 'birds'
delete birds[index];
^
./src/lib.cpp:137:5: error: use of undeclared identifier 'birds'
birds[index]=birds[index+1];
^
./src/lib.cpp:137:18: error: use of undeclared identifier 'birds'
birds[index]=birds[index+1];
^
./src/lib.cpp:140:7: error: cannot assign to non-static data member within const member function 'removeBird'
size--;
~~~~^
./src/lib.cpp:134:13: note: member function 'Array::removeBird' is declared const here
void Array::removeBird(int index)const{
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
./src/lib.cpp:141:3: error: use of undeclared identifier 'birds'
birds[index]=nullptr;
^
你能帮我解决一下吗?
这里的问题是必须包含 class“Bird”动态数组的原始数组中没有任何一个。因为它是这样的
Bird** arr
并在代码中将其更改为正常使用,就像这样
Bird** birds
程序开始正常运行
这是文件“lib.h”
的代码/**
* @file lib.h
* @brief Файл с определением функций для решения заданий из лабораторной работы №23(unready)
*
* @author Taradai S.V.
* @version 0.0
* @date 19-май-2021
*/
#include <iostream>
#include <string>
#include <cstdio>
struct BirdHome{
int HomeArea;
int HomeHeight;
int HomeFeederQuantity;
bool HomeHasNest;
BirdHome();
BirdHome(int homearea, int homeheight, int feederquant, bool nest);
};
class Bird{
private:
bool IsRinged;
std::string Specie;
int BirdAge;
std::string Gender;
struct BirdHome home;
public:
Bird();
Bird(const Bird& copy);
Bird(bool ring,std::string spec, int age,std::string gend, struct BirdHome hinfo);
~Bird();
void set_IsRinged(const bool ring);
void set_Specie(const std::string spec);
void set_BirdAge(const int age);
void set_Gender(const std::string gend);
void set_BirdHome(const struct BirdHome hinfo);
bool get_IsRinged()const;
std::string get_Specie()const;
int get_BirdAge()const;
std::string get_Gender()const;
struct BirdHome get_BirdHome()const;
};
class Array{
private:
Bird** arr;
int size;
int maxsize;
public:
Array();
~Array();
Bird& getBird(int index)const;
void Showall() const;
void ShowBird(Bird bird)const;
void addBird(const Bird& bird);
void removeBird(int index)const;
};
这是文件“lib.cpp”
的代码/**
* @file lib.c
* @brief Файл с реализацией функций для решения заданий из лабораторной работы №23(unready)
*
* @author Taradai S.V.
* @version 0.0
* @date 19-маЙ-2021
*/
#include "lib.h"
BirdHome::BirdHome{
}
BirdHome::BirdHome(int homearea, int homeheight, int feederquant, bool nest)
: HomeArea(homearea),
HomeHeight(homeheight),
HomeFeederQuantity(feederquant),
HomeHasNest(nest){
}
Bird::Bird(){
}
Bird::Bird(const Bird& copy)
: IsRinged(copy.IsRinged),
Specie(copy.Specie),
BirdAge(copy.BirdAge),
Gender(copy.Gender),
home(copy.home.HomeArea,copy.home.HomeHeight,copy.home.HomeFeederQuantity,copy.home.HomeHasNest){
}
Bird::Bird(bool ring,std::string spec, int age,std::string gend, struct BirdHome hinfo)
:IsRinged(ring),
Specie(spec),
BirdAge(age),
Gender(gend),
home(hinfo){
}
Bird::~Bird(){
}
void Bird::set_IsRinged(const bool ring){
IsRinged=ring;
}
void Bird::set_Specie(const std::string spec){
Specie=spec;
}
void Bird::set_BirdAge(const int age){
BirdAge=age;
}
void Bird::set_Gender(const std::string gend){
Gender=gend;
}
void Bird::set_BirdHome(const struct BirdHome hinfo){
home.HomeArea=hinfo.HomeArea;
home.HomeHeight=hinfo.HomeHeight;
home.HomeFeederQuantity=hinfo.HomeFeederQuantity;
home.HomeHasNest=hinfo.HomeHasNest;
}
bool Bird::get_IsRinged()const{
return IsRinged;
}
std::string Bird::get_Specie()const{
return Specie;
}
int Bird::get_BirdAge()const{
return BirdAge;
}
std::string Bird::get_Gender()const{
return Gender;
}
struct BirdHome Bird::get_BirdHome()const{
return home;
}
Array::Array():size(0){
birds=new Bird*[maxsize];
}
Array::~Array(){
for(int i=0;i<maxsize;i++){
delete birds[i];
}
delete[] birds;
}
Bird& Array::getBird(int index)const{
return *birds[index];
}
void Array::Showall() const{
for(int i=0;i<size;i++){
printf("\nBirds %d",i+1);
ShowBird(getBird(i));
}
}
void Array::ShowBird(Bird bird)const{
printf("\n\tIs the birds ringed:%s\n\tThe bird's name specie:%s\n\tThe bird's age is:%d\n\tThe birdd's gender is:%s\n\tThe bird's home area(in square cm) is:%d\n\tThe bird's home height(in cm) is:%d\n\tAmount of feeders in bird's home is:%d\n\tDoes the bird's home has nest:%s",
bird.get_IsRinged()?"true":"false",
bird.get_Specie().c_str(),
bird.get_BirdAge(),
bird.get_Gender().c_str(),
bird.get_BirdHome().HomeArea,
bird.get_BirdHome().HomeHeight,
bird.get_BirdHome().HomeFeederQuantity,
bird.get_BirdHome().HomeHasNest?"true":"false");
}
void Array::addBird(const Bird& bird){
if(size<maxsize){
birds[size]=new Bird(bird);
size++;
}else{
printf("\nThe limit is exceeded");
}
}
void Array::removeBird(int index)const{
delete birds[index];
while (index<size){
birds[index]=birds[index+1];
index++;
}
size--;
birds[index]=nullptr;
}
这是文件“main.cpp”
的代码/**
* @file main.c
* @brief Файл с демонстрацией решения заданий из лабораторной работы №23(unready)
*
* @author Taradai S.V.
* @version 0.0
* @date 19-май-2021
*/
#include "lib.cpp"
int main(){
Array birds;
birds.addBird(Bird(true,"False",5,"Male",BirdHome(100,50,3,false)));
birds.addBird(Bird(false,"Crane",12,"Female",BirdHome(100,100,1,true)));
birds.addBird(Bird(false,"Griffin",36,"Male",BirdHome(500,200,10,true)));
birds.Showall();
printf("\nThat is what we've found at the index 2:");
birds.ShowBird(birds.getBird(2));
printf("\nNow we'll delete the bird at the index 1");
birds.removeBird(1);
printf("This is what we got from it:");
birds.Showall();
return 0;
}
这是文件“Makefile”的代码
all:clean prep compile run format
clean:
rm -rf dist
prep:
mkdir dist
compile:main.bin test.bin
main.bin:
clang++ -g -I./src ./src/main.cpp -o ./dist/main.bin
test.bin:
clang++ -g ./test/test.cpp -o ./dist/test.bin
format:
doxygen Doxyfile
好吧,这个程序应该按以下方式工作:我们有一个 class“Bird”元素的动态数组,其中一个元素有 4 个特征,还有一个包含 4 个特征的结构“BirdHome”当前元素的特征。当 运行 这个程序时,你会自动用一个带参数的构造函数初始化数组中的三个元素(我还有一个默认的构造函数和一个复制的构造函数,但现在不需要它们了)。然后它打印所有这些,然后向您显示具有特定索引的元素,然后删除具有特定索引的元素。然后你会看到这个数组的内容。但是我遇到了 13 个错误,我真的不知道如何修复它们。我只是试图重写某人的代码,但使用我的元素,但出现了这个错误:
In file included from ./src/main.cpp:9:
./src/lib.cpp:11:11: error: qualified reference to 'BirdHome' is a constructor name rather than a type in this context
BirdHome::BirdHome{
^
./src/lib.cpp:11:19: error: expected unqualified-id
BirdHome::BirdHome{
^
./src/lib.cpp:92:3: error: use of undeclared identifier 'birds'
birds=new Bird*[maxsize];
^
./src/lib.cpp:97:12: error: use of undeclared identifier 'birds'
delete birds[i];
^
./src/lib.cpp:99:12: error: use of undeclared identifier 'birds'
delete[] birds;
^
./src/lib.cpp:103:11: error: use of undeclared identifier 'birds'
return *birds[index];
^
./src/lib.cpp:127:5: error: use of undeclared identifier 'birds'; did you mean 'bird'?
birds[size]=new Bird(bird);
^~~~~
bird
./src/lib.cpp:125:33: note: 'bird' declared here
void Array::addBird(const Bird& bird){
^
./src/lib.cpp:127:10: error: type 'const Bird' does not provide a subscript operator
birds[size]=new Bird(bird);
~~~~~^~~~~
./src/lib.cpp:135:10: error: use of undeclared identifier 'birds'
delete birds[index];
^
./src/lib.cpp:137:5: error: use of undeclared identifier 'birds'
birds[index]=birds[index+1];
^
./src/lib.cpp:137:18: error: use of undeclared identifier 'birds'
birds[index]=birds[index+1];
^
./src/lib.cpp:140:7: error: cannot assign to non-static data member within const member function 'removeBird'
size--;
~~~~^
./src/lib.cpp:134:13: note: member function 'Array::removeBird' is declared const here
void Array::removeBird(int index)const{
~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
./src/lib.cpp:141:3: error: use of undeclared identifier 'birds'
birds[index]=nullptr;
^
你能帮我解决一下吗?
这里的问题是必须包含 class“Bird”动态数组的原始数组中没有任何一个。因为它是这样的
Bird** arr
并在代码中将其更改为正常使用,就像这样
Bird** birds
程序开始正常运行