循环依赖项 '{'令牌之前的预期类名

Circular dependency expected class-name before '{' token

本文关键字:令牌 依赖 循环      更新时间:2023-10-16

很抱歉提前启动另一个循环依赖线程,但我几乎尝试了所有方法,也许一双新鲜的眼睛会有所帮助。我如何编译这个 *?

卡。H

#ifndef CARD_H
#define CARD_H
#include <string>
#include <sstream>
#include <irrKlang.h>
#include "Database.h"
using namespace std;
using namespace irrklang;
class Card: public Database{  // problem expected class-name before '{' token
 public:

数据库。H

#ifndef __DATABASE_H__
#define __DATABASE_H__
#include <string>
#include <vector>
#include <sqlite3.h>
#include <wx/string.h>
#include <irrKlang.h>
#include <wx/file.h>
#include "Card.h"   // even though i include card.h
using namespace std;
using namespace irrklang;
class Card;  // if i take this out, I get: 'Card' was not declared in this scope|
class Database
{
public:
vector<Card> queryC(wstring query);

帮助防止循环依赖的两个规则:1.) 如果不需要类的实现,请仅通过前向引用声明它。2.) 如果需要实现,请尽可能晚地包含头文件。

卡.h

#ifndef CARD_H
#define CARD_H
#include "Database.h"
class Card : public Database {
    public:
            int card;
};
#endif // #ifndef CARD_H

数据库.h

#ifndef DATABASE_H
#define DATABASE_H
#include <vector>
#include <string>
class Card;
class Database {
    public:
            std::vector<Card> queryC(std::string query);
};
#endif // #ifndef DATABASE_H

卡.cpp

#include "Card.h"
Card card;

数据库.cpp

#include "Database.h"
Database database;

.

$ g++ -c Card.cpp -o Card.o
$ g++ -c Database.cpp -o Database.o
$ ls -l Card.o Database.o
-rw-r--r-- 1 user group 959 19. Sep 09:13 Card.o
-rw-r--r-- 1 user group 967 19. Sep 09:13 Database.o