簡體   English   中英

C++ 中的未知類型名稱

[英]Unknown type name in C++

我不斷收到“未知類型名稱‘ Life ’”,在world.h和“未知類型名稱‘ Life ’”和“未知類型名稱‘ World ’”在game.h

我有一種感覺,可能有太多基於其他帖子的包含語句,但在這種情況下我找不到解決方案。

任何幫助/提示將不勝感激

游戲.h

#pragma once

#ifndef LIFE_H
#include "life.h"
#define LIFE_H
#endif

#ifndef WORLD_H
#include "world.h"
#define WORLD_H
#endif


class Game {
public:
    // Constructor/destructor
    Game(Life **life, int numLife);
    ~Game();

    void game_loop();
private:
    World * m_world;
    int m_steps;
    bool m_automate;
};

世界.h

#pragma once

#ifndef LIFE_H
    #include "life.h"
    #define LIFE_H
#endif

#ifndef WORLD_H
    #include "world.h"
    #define WORLD_H
#endif

class World {
public:
    // Constructor/destructor
    World();
    ~World();

    void print() const;

    bool initLife(Life *life);
    void updateWorld();
private:

    char getNextState(char state, char row, char col, bool toggle) const;

    char **m_grid_1;
    char **m_grid_2;
    bool m_toggle;
};

life.h(沒有任何錯誤)

#ifndef life_h
#define life_h

#ifndef life_h
    #include "life.h"
    #define life_h
#endif

#ifndef GAME_H
    #include "game.h"
    #define GAME_H
#endif

class Life {
public:

    int getCol() const; // const member functions cannot modify member variables.
    int getRow() const;
    int getHeight() const;
    int getWidth() const;
    char getFromFigure(int r, int c) const;

protected:
    int m_col;
    int m_row;
    int m_height;
    int m_width;
    char **m_sprite;
    World *m_world;
};
#endif

你在這里有效地仔細瞄准並精確地用腳射擊自己:

#ifndef life_h
#define life_h

#ifndef life_h
    #include "life.h"
    #define life_h
#endif

請注意,您定義了阻止包含您自己的頭文件的內容。

這與以下代碼非常相似:

int loaded = 1;

if (!loaded) {
  // Why doesn't this run!?
}

如果您的目標編譯器支持#pragma once則刪除所有這些,並在靠近頂部的每個頭文件中使用#pragma once

否則,您需要單獨對文件進行門控,只需

// example.h
#ifndef EXAMPLE_H
#define EXAMPLE_H

...

#endif

#define的名稱源自文件名並且在整個應用程序中是唯一的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM