-1

I'm trying to learn Inheritance mechanism in C++, I have made a Bancnote(Bills) class, and I want to make a class Card inheriting all the functions and variables from Class Bancnote.

And I get this type of error :

include\Card.h|6|error: expected class-name before '{' token|

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

BANCNOTE.CPP

#include <iostream>
#include "Bancnote.h"
#include "Card.h"
using namespace std;
int Bancnote::sumaplata=0;

Bancnote::Bancnote(string _nume,int _sumacash,int _rest, int _sumaplata )
{
    this->nume=_nume;
    this->sumacash=_sumacash;
    this->rest=_rest;
    this->sumaplata=_sumaplata;
}

Bancnote::Bancnote()
{
    this->nume="";
    this->sumacash=0;
    this->rest=0;
    this->sumaplata=0;
}

Bancnote::~Bancnote()
{
    cout<<"Obiectul"<<"->" <<this->nume<<"<-"<<"a fost sters cu succes";
}

string Bancnote::getnume()
{
    return nume;
}
void Bancnote::setnume(string _nume)
   {
      this->nume=_nume;
   }
int Bancnote::getsumacash()
{
    return sumacash;
}
void Bancnote::setsumacash(int _sumacash)
{
    this->sumacash=_sumacash;
}
int Bancnote::getsumaplata()
{
    return sumaplata;
}
void Bancnote::setsumaplata(int _sumaplata)
{
    this->sumaplata=_sumaplata;
}
int Bancnote::getrest()
{
    return rest;
}
void Bancnote::setrest(int _rest)
{
    this->rest=_rest;
}
void Bancnote::ToString()
{
    cout<< "-----"<<getnume()<< "-----"<<endl;
    cout<<"Suma Cash: "<<this->getsumacash()<<endl;
    cout<<"Suma spre plata: "<<this->getsumaplata()<<endl;
    cout<<"Restul:"<<this->getrest()<<endl;

}

CARD.H

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

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

protected:

private:
};

#endif // CARD_H
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62

1 Answers1

2

You have messed up the includes. What you have is more or less this:

Bancnote.h:

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

struct Bancnote {};
#endif

Card.h

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

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

#endif

When in main you include Bancnote.h then this header includes Card.h so you try to declare Card before Bancnote is declared. Actually Bancnote does not need the definition of Card, so simply removing the include should fix it.

PS: there are other issues (see comments below your question). Most importantly it is not clear why a Card is a Bancnote. Second, never put a using namespace std; inside a header! (see here why)

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185