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
fromcard.h -> <代码>。
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
struct Card : Bancnote {}; // Bancnote is not yet declared
// when compiler reaches here
#endif