C++ C++;不承认.h声明
我在bioter.h(public)中声明了以下函数: 然后,它们在.cc中的实现(为了简短起见,我删除了一堆代码): 但在编译之后,我得到了以下错误:C++ C++;不承认.h声明,c++,header-files,C++,Header Files,我在bioter.h(public)中声明了以下函数: 然后,它们在.cc中的实现(为了简短起见,我删除了一堆代码): 但在编译之后,我得到了以下错误: 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 decla
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)包括“生物.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
bioter.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;
}
}
#包括“bioter.h”
#包括
生物:生物(物种*s,世界*w,点pt,方向d){
世界=w;
物种=s;
loc=pt;
dir=d;
程序线=0;
}
物种*生物::获取物种(){
返回物种;
}
方向:getDirection(){
返回目录;
}
虚空生物::集合物种(物种*s){
物种=s;
}
虚空生物::_hop(){
交换机(dir){
案例北:
如果(!world->getContents(点(位置列,位置行+1))){
世界->设置内容(loc,NULL);
世界->设置内容(点(位置列,位置行+1),此);
}
打破
南方案例:
如果(!world->getContents(点(位置列,位置行-1))){
世界->设置内容(loc,NULL);
世界->设置内容(点(位置列,位置行-1),本);
}
打破
案例东:
如果(!world->getContents(点(位置列+1,位置行))){
世界->设置内容(loc,NULL);
世界->设置内容(点(位置列+1,位置行),此);
}
打破
凯西:
如果(!world->getContents(点(位置列-1,位置行))){
世界->设置内容(loc,NULL);
世界->设置内容(点(位置列1,位置行),本);
}
打破
}
}
虚空生物::_回合(大小\u方向){
如果(方式==0){
交换机(dir){
案例北:
dir=西部;
返回;
凯西:
dir=南部;
返回;
南方案例:
dir=东;
返回;
案例东:
dir=北;
返回;
}
}否则{
交换机(dir){
案例北:
dir=东;
返回;
凯西:
dir=北;
返回;
南方案例:
dir=西部;
返回;
案例东:
dir=南部;
返回;
}
}
}
点生物::_面(){
交换机(dir){
案例北:
返回点(位置列、位置行+1);
打破
凯西:
返回点(位置col-1,位置row);
打破
南方案例:
返回点(位置列,位置行-1);
打破
案例东:
返回点(位置列+1,位置行);
打破
}
}
无效生物::_infect(){
点面=_面();
如果(!world->inRange(facing))返回;
生物*敌人=世界->获取内容(面对);
如果(!敌人)回来;
敌人->固定物种(物种);
世界->设置内容(面对敌人);
}
布尔生物::_isEmpty(){
点面=_面();
如果(!world->inRange(facing))返回false;
如果(!world->getContents(facing))返回true;
返回false;
}
布尔生物::_wall(){
点面=_面();
如果(!world->inRange(facing))返回true;
返回false;
}
布尔生物::_same(){
点面=_面();
如果(!world->inRange(facing))返回true;
如果(!world->getContents(facing))返回false;
生物*敌人=世界->获取内容(面对);
返回(敌人->物种==物种);
}
bool_random(){
int k=random();
收益率(k%2);
}
空生物::takeneturn(){
指令whatToDo=种类->程序步骤(程序行);
开关(whatToDo.op){
案例跳跃:
_hop();
程序线++;
打破
案例左:
_转弯(0);
程序线++;
打破
案例权利:
_转弯(1);
程序线++;
打破
病例感染:
_感染();
程序线++;
打破
案例IFEMPTY:
如果(_isEmpty()){
programLine=whatToDo.line;
takeneturn();
}
打破
案例IFWALL:
如果(_wall()){
programLine=whatToDo.line;
takeneturn();
}
打破
情况同上:
如果(_same()){
programLine=whatToDo.line;
takeneturn();
}
打破
案例GO:
programLine=whatToDo.line;
takeneturn();
打破
个案编号:
如果(_random())programLine=whatToDo.line;
else-programLine++;
takeneturn();
打破
}
}
呸 你的头文件说这些函数是独立的,但你的源文件说它们属于“生物”类。至少,您需要:
- 用结构或类包围您的声明 结构生物 { …您的函数声明。。。 };
- 从您的cpp文件中删除生物::
最后,在函数名开头加上未加分数是不好的做法。不要这样做。你的头文件说这些函数是独立的,但你的源文件说它们属于“生物”类。至少,您需要:
- 用结构或类包围您的声明 结构生物 { …您的函数声明。。。 };
- 从您的cpp文件中删除生物::
最后,在函数名开头加上未加分数是不好的做法。不要这样做。看起来您正在尝试编写一个类。您必须将函数声明放在类声明中,然后将头包含在源文件中 生物
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 ;
bioter.cc
#include "Creature.h"
... your original Creature.cc contents ...
如果您不是在编写一个类,而是在编写一系列自由函数,请从
bioture.cc
中的函数名中删除bioture::
。您必须将函数声明放在类声明中,然后将头包含在源文件中
生物
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 ;
bioter.cc
#include "Creature.h"
... your original Creature.cc contents ...
如果你不是在写一个类而是一个系列
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
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
#include <cstring>
typedef int Species;
typedef int World;
typedef int Point;
typedef int Direction;
class Creature {
#error The class declaration for Creature is indeed being compiled