C++ 类和构造函数有很多错误和问题
下面是文件“lib.h”的代码 下面是文件“Makefile”的代码 这个程序应该以如下方式工作:我们有一个“Bird”类元素的动态数组,它有一个元素的4个特征,还有一个结构“BirdHome”,它包含当前元素的4个特征。运行此程序时,您将使用带参数的构造函数自动初始化数组中的三个元素(我还有一个默认contsructor和一个复制contsructor,但现在不需要它们)。然后它会打印所有元素,然后显示一个具有特定索引的元素,然后删除一个具有特定索引的元素。然后会显示此数组的内容。但我有13个错误,我真的不知道如何修复它们。我只是试图重写某人的代码,但使用了我的元素,但出现了以下错误:C++ 类和构造函数有很多错误和问题,c++,arrays,class,struct,dynamic,C++,Arrays,Class,Struct,Dynamic,下面是文件“lib.h”的代码 下面是文件“Makefile”的代码 这个程序应该以如下方式工作:我们有一个“Bird”类元素的动态数组,它有一个元素的4个特征,还有一个结构“BirdHome”,它包含当前元素的4个特征。运行此程序时,您将使用带参数的构造函数自动初始化数组中的三个元素(我还有一个默认contsructor和一个复制contsructor,但现在不需要它们)。然后它会打印所有元素,然后显示一个具有特定索引的元素,然后删除一个具有特定索引的元素。然后会显示此数组的内容。但我有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;
^
你能帮我解决它们吗?这里的问题是,原始数组中肯定包含了类“Bird”的动态数组,但它没有。因为是这样的
Bird** arr
通过将其更改为代码中的正常使用方式,就像下面这样
Bird** birds
程序开始正常工作第一个错误是由于缺少括号:
BirdHome::BirdHome{
-->BirdHome::BirdHome(){
和Array
没有名为birds
的成员。哇,你真的帮了我的忙。我忘了我做了Bird**arr
而不是Bird**birds
我的建议是从顶部开始读取错误。很多时候,一个问题可能会触发多个错误。void removeBird(int index)const;
如果removeBird()
要对对象进行更改,则需要在末尾删除const
。包含“lib.cpp”
从不包含.cpp文件。..h文件适合包含。
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
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;
^
Bird** arr
Bird** birds