【问题标题】:variable or field `generateMonster' declared void [duplicate]变量或字段“generateMonster”声明为无效[重复]
【发布时间】:2012-09-25 20:58:04
【问题描述】:

可能重复:
variable or field declared void

我正在做一个项目,但遇到了一些功能问题。我不断收到一个错误,我知道这意味着我从未声明过类型,但我知道我是。所有文件都包含 .h 和 .cpp,所以我没有任何缺失的包含。

这是我调用它的函数 - 问题是 generateMonster

int room1(charType& heroObj) {

    int r;
    roomType RoomObj;
    RoomObj.generateMonster(heroObj);

    cout << "There's a hiss as you open this door, and you smell a sour odor, like something rotten or fermented. Inside you see a small room lined with dusty shelves, crates, and barrels. It looks like someone once used this place as a larder, but it has been a long time since anyone came to retrieve food from it." << endl;
    cout << endl << "Room #1:\n 1. Door in the East corner of the North wall.\n 2. Door in the middle of the East wall.\n 3. Door in the east corner of the south wall.\n 4. Door in the West corner of the South wall.\n" << endl;
    doIt();
    cin >> r;
    if (!cin.good())
       cout << "That is not a valid option.  Please try again.";
       cin.clear();
       cin.ignore(100, '\n');

    system("cls");

    switch(r) {
              case 1:
                   genDoor();
                   hallway1(heroObj);
                   break;
              case 2:
                   genDoor();
                   hallway4(heroObj);
                   break;
              case 3:
                   genDoor();
                   hallway3(heroObj);
                   break;
              case 4:
                   genDoor();
                   hallway2(heroObj);
                   break;
    }

}

我的原型

class roomType
    {
    public:
     roomType();
     void generateMonster(charType& heroObj);
     void generateTreasure();

    private:
     int gold;
     int potion;
     int keys;
    };

和我的整体职能 - http://pastie.org/4799752

【问题讨论】:

  • 也许您应该复制/粘贴确切的错误消息。
  • 我看到了,但是我声明的类型不是 std: 的成员,而是一个类。
  • @VernBurton 问题是编译器不知道你在说什么类型。不提供命名空间只是导致此错误消息的一种方式。在您的情况下,我认为可能缺少#include(请参阅我的回答)。

标签: c++


【解决方案1】:

基于this question,问题似乎是编译器不知道charType 是在哪里声明generateMonster(charType&amp;)。可能是#include "charType.h"class roomType 之前?

【讨论】:

  • .h 文件已包含在内,但问题在于它们的顺序。当然,事情就是这么简单。
  • @VernBurton 要解决这个问题,您应该在roomType.h 中执行#include "charType.h"。您还需要使用include guards 来防止在多次包含文件时出错。
  • 谢谢。这是我第一次尝试编译语言,到目前为止我不能抱怨。
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 2021-12-20
  • 1970-01-01
  • 2014-05-08
  • 1970-01-01
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
相关资源
最近更新 更多