简体   繁体   English

C ++无法识别.h声明

[英]C++ not recognizing .h declarations

I've got the following functions declared in creature.h (under public): 我在generic.h中声明了以下函数(在公共环境中):

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

Then their implementation in the .cc (I've deleted a bunch of code for shortness): 然后在.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
}

But then upon compile I get these errors: 但是然后在编译时出现以下错误:

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'

tons of other functions are getting read fine but these ones aren't getting any love. 大量其他功能都可以很好地读取,但是这些功能并没有得到任何喜爱。 ????? ?????

EDIT: 编辑:

Not sure about the down votes, probably because you guys are assuming that I didn't either a) include it in the creature class or b) #include "creature.h". 不确定否决票,可能是因为你们假设我没有a)将其包括在生物类别中,或b)#include“ creature.h”。 I did both, just didn't include that in the question since I thought that was obvious. 我都做了,只是没有将其包括在问题中,因为我认为这很明显。

EDIT 2: You want .cc and .h? 编辑2:您想要.cc和.h吗? Oh dear lord. 哦,天哪。

CREATURE.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 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;
   }
 }

Phew! 唷!

What you have described should work fine, so we have to assume that your description doesn't quite match reality. 你所描述什么应该可以正常工作,所以我们必须假设你的描述不完全匹配的现实。

Specifically, the following code compiles and runs fine. 具体来说,以下代码可以编译并正常运行。

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

Since that transcript shows what you seem to be doing, my advice is to post your actual code for analysis, including the full header file and almost full source file (you can leave in the CODE markers since they won't affect this particular problem but it would be useful to see things like the include statements and so forth). 由于该笔录显示了您似乎在做什么,我的建议是发布您的实际代码进行分析,包括完整的头文件和几乎完整的源文件(您可以保留在CODE标记中,因为它们不会影响此特定问题,但是查看诸如include语句之类的内容很有用)。

The best problem reports should come with: 最好的问题报告应随附:

  • the expected behaviour. 预期的行为。
  • the actual behaviour. 实际行为。
  • a small complete program that exhibits the errant behaviour. 展示错误行为的完整小程序。

Based on your code updates (and assuming they're complete), you have the problem that size_t , Species , World , Point and Direction are not actually defined before you include creature.h in your creature.cc file. 基于您的代码更新(并假设他们完成),你有这个问题size_tSpeciesWorldPointDirection都没有真正定义你之前包含creature.hcreature.cc文件。

This is causing the creation of the Creature class to fail on the second constructor so that it doesn't know about that class. 这导致第二个构造函数上Creature类的创建失败,因此它不知道该类。 Then, when you try to define the actual code for said class, the compiler (rightly) complains that it doesn't exist. 然后,当您尝试为所述类定义实际代码时,编译器(正确地)会抱怨它不存在。

When I add a very small main to your code and try to compile, I get these header file problems: 当我在代码中添加一个很小的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

which are caused by those non-definitions. 由这些未定义引起的。

Then I see the sort of errors you describe, such as: 然后,我看到您描述的错误类型,例如:

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

(just a sample of the many pages of output). (仅是输出的许多页面中的一个示例)。

You need to ensure that you have included the header files which define those types before including creature.h . 您需要确保在包含creature.h之前,已包含定义这些类型的头文件。 Ideally, creature.h would do this itself rather than relying on whatever is using it to do it. 理想情况下, creature.h会自己执行此操作,而不是依赖于使用它执行的操作。

size_t can be found in <cstring> , the others need to be found by yourself. size_t可以在<cstring>找到,其他则需要您自己找到。 For example, placing: 例如,放置:

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

at the top of your header file gets rid of all those errors - it introduces a bucketload of other errors since the typedef statements are wrong but I'm just illustrating what you need to do here. 头文件顶部的所有错误均已消除-由于typedef语句错误,因此引入了其他错误,但我只是在这里说明您需要执行的操作。

Find out where those other things are defined and include them, along with <cstring> , at the top of creature.h . 找出其他内容的定义位置,并将它们与<cstring>一起包括在creature.h <cstring>的顶部。

It looks like you're trying to write a class. 看来您正在尝试编写一个类。 You have to put the function declarations in a class declaration, then include the header in the source file. 您必须将函数声明放入类声明中,然后在源文件中包含标头。

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 Creature.cc

#include "Creature.h"

... your original Creature.cc  contents ...

If you're not writing a class but a series of free functions, remove the Creature:: from your function names in Creature.cc . 如果你不写一个类,而是一系列的免费功能,去除Creature::从你的函数名Creature.cc

Your header file says that the functions are standalone, but your source file says they belong to the "Creature" class. 您的头文件说这些功能是独立的,但是您的源文件说它们属于“ Creature”类。 At the very minimum, you need to either: 至少,您需要:

  • surround your declarations with a struct or class 用结构或类包围声明

    struct Creature { ....your function declarations... }; struct Creature {....您的函数声明...};

or 要么

  • Remove the Creature:: from your cpp file. 从您的cpp文件中删除Creature ::。

Finally, starting function names with an undescore is bad practice. 最后,以undescore开头的函数名称是不好的做法。 Don't do it. 不要这样

Just before 就在之前

class Creature {

try adding the following: 尝试添加以下内容:

#error The class declaration for Creature is indeed being compiled

And see if that generates an error. 看看是否会产生错误。 If not, then I suspect something went wrong with your header guard. 如果不是,那么我怀疑您的标题卫队出了点问题。

Your header guard macro name should be longer than just CREATURE . 标头保护程序宏名称应比CREATURE It's too easy for a CREATURE macro to be accidentally defined somewhere else (like in a library) and messing up your header guard. 一个CREATURE宏很容易被意外地定义在其他地方(例如在库中)并弄乱了您的标题保护。 If the name of your project is, say, "Monster Mash", then your header guard for creature.h should be something complicated like: MONSTER_MASH_CREATURE_H or MONSTER_MASH_CREATURE_INCLUDE . 如果您的项目名称为“ Monster Mash”,则您的creature.h的标头保护应该类似于: MONSTER_MASH_CREATURE_HMONSTER_MASH_CREATURE_INCLUDE That way, it's extremely improbable that someone else has already defined your header guard macro. 这样,其他人已经定义了标头保护宏是极不可能的。

Move #include "creature.h" to the TOP of the list of includes in your creature.cc file. 将#include“ creature.h”移到生物.cc文件中包含列表的顶部。 Then, fix the compile errors (types 'Point', etc. not defined), and try again. 然后,修复编译错误(类型“ Point”等未定义),然后重试。 The creature.h file needs to include or forward-declare all the types it uses, or you get weird problems. 生物.h文件需要包括或转发声明它使用的所有类型,否则您会遇到奇怪的问题。

I also second the suggestion to put a #error just above the Creature class declaration, to ensure that CREATURE didn't somehow get defined elsewhere (and thus force the class declaration to be skipped). 我还建议将#error放在Creature类声明的上方,以确保CREATURE不会在其他地方定义(从而迫使类声明被跳过)。 And you might want a more unique header on your header file -- my own convention would have been _CREATURE_H_ 而且您可能希望在头文件上使用更独特的头-我自己的约定是_CREATURE_H_

Anyway, the upshot is that most of these kinds of bugs are caused by problems somewhere else, and the symptoms are only distantly related to the reason. 无论如何,结果是大多数这类错误是由其他地方的问题引起的,并且症状仅与原因有很远的关系。 C++, alas, is loaded with these kinds of gotchas. ++,C ++充满了这类陷阱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM