簡體   English   中英

類和構造函數的很多錯誤和問題

[英]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“鳥”元素的動態數組,它有一個元素的 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

程序開始正常運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM