c++不识别.h声明
C++ not recognizing .h declarations
我已经在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) #包括"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
生物。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 << "Hopn";
}
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
和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>
一起,在creature.h
的顶部
看起来你想写一个类。您必须将函数声明放在类声明中,然后将头文件包含在源文件中。
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
函数名中去掉
你的头文件说这些函数是独立的,但是你的源文件说它们属于"Creature"类。至少,您需要:
-
用一个结构体或类包围你的声明
结构生物{.…你的函数声明…};
或
- 从你的cpp文件中删除Creature::
最后,以非核心开头的函数名是不好的做法。不要这样做。
在
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
。这样,别人已经定义了您的标题保护宏的可能性极小。
移动#include "creature.h"到你的生物包含列表的顶部。cc文件。然后,修复编译错误(未定义类型'Point'等),然后重试。h文件需要包含或前向声明它使用的所有类型,否则你会遇到奇怪的问题。
我还支持在Creature类声明上方放置#error的建议,以确保Creature不会以某种方式在其他地方被定义(从而强制跳过类声明)。您可能希望头文件有一个更独特的头——我自己的约定是_CREATURE_H_
无论如何,结果是大多数这类bug都是由其他地方的问题引起的,而症状与原因只有很小的关系。唉,c++充满了这些陷阱。
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 提升 ASIO 无法识别计时器对象
- 从udp接收帧对于人脸识别来说太慢
- 错误:未在此范围内声明'reverse'
- 编译器无法识别在类声明之外定义的方法
- 无法识别的模板声明错误 - 不明白我做错了什么
- 实现文件只能识别其他类的远期声明
- 在同一项目 (Cork+VTK) 中使用 2 个库时C++无法识别的模板声明
- auto f = bind(dist<>,gen) 在成员声明C++失败。未被识别为函数
- MSVC:无法识别的模板声明/定义(使用 Clang/GCC 编译)
- 声明cudaError_t变量时出现无法识别的令牌错误
- 来自无法识别的继承结构的本地声明
- 人脸识别器未在此范围内声明
- 仅在 Eclipse 中不识别在一个位置的 ofstream 声明
- 无法识别的模板声明/定义
- c++不识别.h声明
- C2988:无法识别的模板声明/定义