我在creature.h中声明了以下函数(在公共场所下):
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?亲爱的主啊。
CREATURE.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
CREATURE.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;
}
}
呼!
答案 0 :(得分:3)
你所描述的应该可以正常工作,所以我们必须假设你的描述完全匹配现实。
具体来说,以下代码编译并运行正常。
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
,Point
和{{1}在Direction
文件中包含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>
的顶部。
答案 1 :(得分:1)
看起来你正在尝试写一堂课。您必须将函数声明放在类声明中,然后在源文件中包含标题。
Creature.h
class Creature {
private: // assuming these are private functions
void _hop();
void _turn(size_t way);
void _infect();
Point _facing();
bool _isEmpty();
bool _wall();
bool _same();
}; // don't forget the ;
Creature.cc
#include "Creature.h"
... your original Creature.cc contents ...
如果您不是在编写课程而是编写一系列免费函数,请从Creature::
中的函数名称中删除Creature.cc
。
答案 2 :(得分:0)
你的头文件说这些函数是独立的,但你的源文件说它们属于“Creature”类。至少,你需要:
使用结构或类
包围您的声明struct Creature { ....你的函数声明...... };
或
最后,使用非核心启动函数名称是不好的做法。不要这样做。
答案 3 :(得分:0)
就在
之前class Creature {
尝试添加以下内容:
#error The class declaration for Creature is indeed being compiled
看看是否会产生错误。如果没有,那么我怀疑你的头部防守出了问题。
您的标头保护宏名称应该比CREATURE
更长。 CREATURE
宏在其他地方(例如在库中)被意外定义并且弄乱你的头部防护,这太容易了。如果您的项目名称是“Monster Mash”,那么creature.h
的标题保护应该是复杂的:MONSTER_MASH_CREATURE_H
或MONSTER_MASH_CREATURE_INCLUDE
。这样,其他人已经定义了你的标题保护宏非常不可能。
答案 4 :(得分:0)
将#include“creature.h”移动到creature.cc文件中包含列表的TOP。然后,修复编译错误(类型'Point'等未定义),然后重试。 creature.h文件需要包含或转发声明它使用的所有类型,否则会出现奇怪的问题。
我还提出了将#error放在Creature类声明之上的建议,以确保CREATURE不会以某种方式在别处定义(从而强制跳过类声明)。你可能想在头文件上有一个更独特的标题 - 我自己的约定是_CREATURE_H _
无论如何,结果是大多数这类错误都是由其他地方的问题引起的,症状与原因有很大关系。唉,C ++充满了这些陷阱。