C++;不同文件中的类继承 我尝试学习C++中的继承机制,我制作了一个BANNECK(BARS)类,我想制作一个类卡继承BANNECK类的所有函数和变量。

C++;不同文件中的类继承 我尝试学习C++中的继承机制,我制作了一个BANNECK(BARS)类,我想制作一个类卡继承BANNECK类的所有函数和变量。,c++,inheritance,C++,Inheritance,我得到了这种类型的错误: include\Card.h | 6 |错误:在“{”标记之前应该有类名| BANCNOTE.H #ifndef BANCNOTE_H #define BANCNOTE_H #include <iostream> #include "Card.h" using namespace std; class Bancnote { public: Bancnote(); Bancnote(string, int ,i

我得到了这种类型的错误:

include\Card.h | 6 |错误:在“{”标记之前应该有类名|

BANCNOTE.H

   #ifndef BANCNOTE_H
#define BANCNOTE_H
#include <iostream>

#include "Card.h"
using namespace std;

class Bancnote
{
    public:
        Bancnote();
        Bancnote(string, int ,int ,int );
        ~Bancnote( );
        int getsumacash( );
        void setsumacash( int );
        int getsumaplata( );
        void setsumaplata( int );
        int getrest( );
        void setrest( int );
        string getnume( );
        void setnume( string );
        void ToString();


    protected:

    private:
        string nume;
        int sumacash;
        int rest;
        static int sumaplata;


};

#endif // BANCNOTE_H
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"

class Card: public Bancnote
{
public:
    Card();
    virtual ~Card();

protected:

private:
};

#endif // CARD_H

你搞乱了包含的内容。你所拥有的或多或少是这样的:

Bancnote.h:

#ifndef BANCNOTE_H
#define BANCNOTE_H
#include "Card.h"    // remove this

struct Bancnote {};
#endif
卡片

#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"

struct Card : Bancnote {};   // Bancnote is not yet declared
                             // when compiler reaches here

#endif
当您主要包含
Bancnote.h
时,此标题包含
Card.h
,因此您尝试在声明
Bancnote
之前声明
Card
。实际上
Bancnote
不需要
Card
的定义,因此只需删除包含项即可解决此问题


PS:还有其他问题(请参见问题下方的注释)。最重要的是,不清楚为什么
卡是
Bancnote
。其次,不要将
使用名称空间std;
放在标题中!(请参见原因)

问题是什么?我想是“如何修复错误?”,但我们需要查看代码。请阅读标题中的
使用名称空间std;
。不要从
bancnote.h
中包含
card.h
。其效果是在定义
bancnote
之前,尝试从
bancnote
派生
card
。包括
bancnote.h
from
card.h -> <代码>。
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"

struct Card : Bancnote {};   // Bancnote is not yet declared
                             // when compiler reaches here

#endif