【问题标题】:A lot of errors and problems with classes and constructors类和构造函数的很多错误和问题
【发布时间】:2021-08-09 18:11:31
【问题描述】:

这是文件“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

好吧,这个程序应该以下列方式工作:我们有一个动态数组的“鸟”类元素,它有一个元素的 4 个特征,还有一个结构“BirdHome”,它包含当前元素的 4 个特征。运行此程序时,您将使用带参数的构造函数自动初始化数组中的三个元素(我还有一个默认构造函数和一个复制构造函数,但现在不需要它们)。然后它会打印所有这些,然后显示具有特定索引的元素,然后删除具有特定索引的元素。然后你会看到这个数组的内容。但我有 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;
  ^

你能帮我解决它们吗?

【问题讨论】:

  • 第一个错误是由于缺少括号:BirdHome::BirdHome{ --> BirdHome::BirdHome () { 和 Array 没有名为 birds 的成员。
  • 哇,你真的帮了我。我忘了我做了一个Bird** arr 而不是Bird** birds
  • 我的建议是从顶部开始阅读错误。很多时候,一个问题可能会引发多个错误。
  • void removeBird(int index)const; 如果removeBird() 要对对象进行更改,则需要删除末尾的const。
  • #include "lib.cpp" 从不包含 .cpp 文件。 .h 文件适合包含在内。

标签: c++ arrays class struct dynamic


【解决方案1】:

这里的问题是必须包含类“Bird”的动态数组的原始数组没有任何它。因为是这样的

Bird** arr

并通过在代码中将其更改为正常使用,例如这个

Bird** birds

程序开始正常运行

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    相关资源
    最近更新 更多