0

I have a class (ShowTicket) in a header file (showticket.h) and it outputs no errors on its own like so:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
    class ShowTicket {
    public:
        //function that returns true if sold status is true and false if it doesnt.
        bool is_sold(void){
            return sold_status;
        }
        //function that sets sold_status to true
        void sell_seat(void){
            sold_status = true;
        }
        //prints row, seat number and sold status in casual terms
        string print_ticket(void){
            ostringstream sout;
            if(sold_status == true){
                sout<<row<<" "<<seat_number<<" sold";
            }
            else{
                sout<<row<<" "<<seat_number<<" available";
            }
            return sout.str();
        }
        //initilizes variables in constructor
        bool sold_status;
    //default constructor
        ShowTicket();
    //constructor
        ShowTicket(const char* Row, const char* SeatNumber):
        sold_status{false},
        row(Row),
        seat_number(SeatNumber)
        {}
        
    private:
        const char* row;
        const char* seat_number;
    };

I then have a main function in a cpp file here:

#include <iostream>
#include "showticket.h"
#include <sstream>
#include "sportticket.h"

int main(){
    SportTicket myticket1("AA","101");
    SportTicket myticket2("AA","102");
    myticket1.sell_seat();
    myticket2.sell_seat();
    myticket2.sell_beer();
    cout<<myticket1.print_ticket() << endl;
    cout<<myticket2.print_ticket()<<endl;
    return 0;
}

After creating this main function the header file receieves this error "redefinition of 'ShowTicket'"

How can I fix this?

Other header file as requested:

#include <iostream>
#include <sstream>
#include "showticket.h"
//creation of derived class
class SportTicket: public ShowTicket{
public:

    //Constructor
    SportTicket(const char* RowSport, const char* SeatNumberSport):
    ShowTicket(),
    beer_sold_check{false}
    
    {}
    
    //initialize new variables for this class
    bool beer_sold_check;
    const char* RowSport;
    const char* SeatNumberSport;
    
    bool beer_sold(){
        return beer_sold_check;
    }
    
    void sell_beer(){
        beer_sold_check = true;
    }
    
    //prints row, seat number and sold status in casual terms
    string print_ticket(void){
        ostringstream sout;
        if(sold_status == true && beer_sold_check == true){
            sout<<RowSport<<" "<<SeatNumberSport<<" sold beer";
        }
        else if(sold_status == true && beer_sold_check == false){
            sout<<RowSport<<" "<<SeatNumberSport<<" sold nobeer";
        }
        else if(sold_status == false && beer_sold_check == true){
            sout<<RowSport<<" "<<SeatNumberSport<<" available beer";
        }
        else if(sold_status == false && beer_sold_check == false){
            sout<<RowSport<<" "<<SeatNumberSport<<" available nobeer";
        }
        return sout.str();
    }
};-=-\





Flann3l
  • 69
  • 5
  • And what's in the other header file? P.S.: `using namespace std` especially in a header file, always ends in tears. – Sam Varshavchik May 23 '22 at 23:26
  • If these are the full contents of the header file, you forgot to give it a header guard. These should be present in _every_ header file. – user17732522 May 23 '22 at 23:27
  • @SamVarshavchik Added the additional header file. I also removed the namespace since it only mattered on four lines and I don't believe it uncovered anything that could be the problem. – Flann3l May 23 '22 at 23:33
  • Of course -- the header file gets included two times, the 2nd time from the other header file, so what exactly is unclear to you about the duplicate definition error from your compiler? – Sam Varshavchik May 24 '22 at 00:02

0 Answers0