gpt4 book ai didi

C++ 不识别 .h 声明

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

我在 creature.h 中声明了以下函数(在 public 下):

void _hop();
void _turn(size_t way);
void _infect();
Point _facing();
bool _isEmpty();
bool _wall();
bool _same();

然后他们在 .cc 中的实现(为了简洁我删除了一堆代码):

void Creature::_hop(){
CODE
}

Point Creature::_facing(){
CODE
}

void Creature::_infect(){
CODE
}

bool Creature::_isEmpty(){
CODE
}

bool Creature::_wall(){
CODE
}

bool Creature::_same(){
CODE
}

但是在编译时我得到了这些错误:

creature.cc:25: error: no 'void Creature::_hop()' member function declared in class 'Creature'
creature.cc:54: error: no 'void Creature::_turn(size_t)' member function declared in class 'Creature'
creature.cc:88: error: no 'geometry::Point Creature::_facing()' member function declared in class 'Creature'
creature.cc:105: error: no 'void Creature::_infect()' member function declared in class 'Creature'
creature.cc:114: error: no 'bool Creature::_isEmpty()' member function declared in class 'Creature'
creature.cc:121: error: no 'bool Creature::_wall()' member function declared in class 'Creature'
creature.cc:127: error: no 'bool Creature::_same()' member function declared in class 'Creature'

许多其他功能都得到了很好的阅读,但这些功能却没有得到任何喜爱。 ??????

编辑:

不确定否决票,可能是因为你们假设我没有 a) 将它包含在生物类中或 b) #include "creature.h"。我两个都做了,只是没有在问题中包括它,因为我认为这是显而易见的。

编辑 2:您想要 .cc 和 .h?哦,亲爱的主。

怪物.H:

 #ifndef CREATURE
#define CREATURE
class Creature {
public:
// Constructor (note there is no need for a destructor.)
Creature();
Creature(Species *species,World *world,Point pt,Direction d);

// takeOneTurn executes lines of this creature's species program,
// beginning on programLine and continuing until a HOP, LEFT, RIGHT, or
// INFECT is executed.
void takeOneTurn();

// getters and setters do the obvious things.
Species *getSpecies();
Direction getDirection();
// use this to initialize and infect. It also sets programLine to 1.
void setSpecies(Species * s);
void _hop();
void _turn(size_t way);
void _infect();
Point _facing();
bool _isEmpty();
bool _wall();
bool _same();

private:
Species *species; // pointer to this creature's species
World *world; // a pointer to the world in which this
// creature is located.
Point loc; // where in the world this creature is located
Direction dir; // current direction this creature is facing
size_t programLine; // current program line
};

#endif

生物.CC

 #include "creature.h"
#include <cstdlib>

Creature::Creature(Species *s, World *w,Point pt,Direction d){
world = w;
species = s;
loc = pt;
dir = d;
programLine = 0;
}


Species* Creature::getSpecies(){
return species;
}

Direction Creature::getDirection(){
return dir;
}

void Creature::setSpecies(Species* s){
species = s;
}

void Creature::_hop(){
switch(dir){
case NORTH:
if(!world->getContents(Point(loc.col,loc.row+1))){
world->setContents(loc, NULL);
world->setContents(Point(loc.col,loc.row+1), this);
}
break;
case SOUTH:
if(!world->getContents(Point(loc.col,loc.row-1))){
world->setContents(loc, NULL);
world->setContents(Point(loc.col,loc.row-1), this);
}
break;
case EAST:
if(!world->getContents(Point(loc.col+1,loc.row))){
world->setContents(loc, NULL);
world->setContents(Point(loc.col+1,loc.row), this);
}
break;
case WEST:
if(!world->getContents(Point(loc.col-1,loc.row))){
world->setContents(loc, NULL);
world->setContents(Point(loc.col-1,loc.row), this);
}
break;
}
}

void Creature::_turn(size_t way){
if(way == 0){
switch(dir){
case NORTH:
dir = WEST;
return;
case WEST:
dir = SOUTH;
return;
case SOUTH:
dir = EAST;
return;
case EAST:
dir = NORTH;
return;
}
} else {
switch(dir){
case NORTH:
dir = EAST;
return;
case WEST:
dir = NORTH;
return;
case SOUTH:
dir = WEST;
return;
case EAST:
dir = SOUTH;
return;
}
}
}

Point Creature::_facing(){
switch(dir){
case NORTH:
return Point(loc.col,loc.row+1);
break;
case WEST:
return Point(loc.col-1,loc.row);
break;
case SOUTH:
return Point(loc.col,loc.row-1);
break;
case EAST:
return Point(loc.col+1,loc.row);
break;
}
}

void Creature::_infect(){
Point facing = _facing();
if(!world->inRange(facing))return;
Creature* enemy = world->getContents(facing);
if(!enemy) return;
enemy->setSpecies(species);
world->setContents(facing, enemy);
}

bool Creature::_isEmpty(){
Point facing = _facing();
if(!world->inRange(facing))return false;
if(!world->getContents(facing)) return true;
return false;
}

bool Creature::_wall(){
Point facing = _facing();
if(!world->inRange(facing))return true;
return false;
}

bool Creature::_same(){
Point facing = _facing();
if(!world->inRange(facing))return true;
if(!world->getContents(facing)) return false;
Creature* enemy = world->getContents(facing);
return (enemy->species == species);
}

bool _random(){
int k = random();
return (k%2);
}

void Creature::takeOneTurn(){
Instruction whatToDo = species->programStep(programLine);
switch(whatToDo.op){
case HOP:
_hop();
programLine++;
break;
case LEFT:
_turn(0);
programLine++;
break;
case RIGHT:
_turn(1);
programLine++;
break;
case INFECT:
_infect();
programLine++;
break;
case IFEMPTY:
if(_isEmpty()){
programLine = whatToDo.line;
takeOneTurn();
}
break;
case IFWALL:
if(_wall()){
programLine = whatToDo.line;
takeOneTurn();
}
break;
case IFSAME:
if(_same()){
programLine = whatToDo.line;
takeOneTurn();
}
break;
case GO:
programLine = whatToDo.line;
takeOneTurn();
break;
case IFRANDOM:
if(_random()) programLine = whatToDo.line;
else programLine++;
takeOneTurn();
break;
}
}

呸!

最佳答案

您所描述的应该没问题,因此我们不得不假设您的描述完全与现实不符。

具体而言,以下代码可以正常编译和运行。

pax$ cat creature.h
class Creature {
public:
void _hop();
};

pax$ cat creature.cc
#include <iostream>
#include "creature.h"

void Creature::_hop() {
std::cout << "Hop\n";
}

int main (void) {
Creature c;
c._hop();
return 0;
}

pax$ rm creature ; g++ -o creature creature.cc ; ./creature
Hop

由于该记录显示了您似乎在做什么,我的建议是发布您的实际代码以供分析,包括完整头文件和几乎完整的源文件(您可以保留 CODE 标记,因为它们不会影响此特定问题,但查看 include 语句等内容会很有用)。

最好的问题报告应该包括:

  • 预期的行为。
  • 实际行为。
  • 展示错误行为的完整小程序。

根据您的代码更新(并假设它们已完成),您遇到的问题是 size_t , Species , World , PointDirection在包含 creature.h 之前实际上并未定义在你的creature.cc文件。

这会导致创建 Creature类在第二个构造函数上失败,这样它就不会知道那个类。然后,当您尝试为所述类定义实际代码时,编译器(正确地)提示它不存在。

当我添加一个非常小的main到您的代码并尝试编译,我遇到了这些头文件问题:

In file included from creature.cc:1:
creature.h:5: error: expected `)' before '*' token
creature.h:13: error: ISO C++ forbids declaration of 'Species' with no type
creature.h:13: error: expected ';' before '*' token
creature.h:14: error: 'Direction' does not name a type
creature.h:16: error: 'Species' has not been declared
creature.h:18: error: 'size_t' has not been declared
creature.h:20: error: 'Point' does not name a type
creature.h:26: error: ISO C++ forbids declaration of 'Species' with no type
creature.h:26: error: expected ';' before '*' token
creature.h:27: error: ISO C++ forbids declaration of 'World' with no type
creature.h:27: error: expected ';' before '*' token
creature.h:29: error: 'Point' does not name a type
creature.h:30: error: 'Direction' does not name a type
creature.h:31: error: 'size_t' does not name a type

这是由那些非定义引起的。

然后我看到你描述的那种错误,例如:

creature.cc:129: error: 'world' was not declared in this scope
creature.cc:129: error: 'facing' was not declared in this scope
creature.cc:130: error: 'world' was not declared in this scope
creature.cc:130: error: 'facing' was not declared in this scope
creature.cc:131: error: 'world' was not declared in this scope
creature.cc:131: error: 'facing' was not declared in this scope
creature.cc:132: error: 'class Creature' has no member named 'species'
creature.cc:132: error: 'species' was not declared in this scope

(只是许多输出页面的示例)。

在包含 creature.h 之前,您需要确保包含了定义这些类型的头文件。 .理想情况下,creature.h会自己做这件事,而不是依赖使用它的任何东西。

size_t可以在 <cstring> 中找到,其他的需要自己找。例如,放置:

#include <cstring>
typedef int Species;
typedef int World;
typedef int Point;
typedef int Direction;

在你的头文件的顶部摆脱了所有这些错误 - 它引入了自 typedef 以来的一大堆其他错误陈述是错误的,但我只是在说明您需要在此处做什么。

找出那些其他东西的定义位置,并将它们与 <cstring> 一起包括在内, 在 creature.h 的顶部.

关于C++ 不识别 .h 声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7989293/

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