Home > Net >  Undefined Symbol: Class::Class() error in C . How can I fix this?
Undefined Symbol: Class::Class() error in C . How can I fix this?

Time:05-25

I have a header file with two classes in it:

//
//  sportandshowticket.h
//  sportticket  test
//
//  Created by Daniel Reinoso on 5/24/22.
//

#ifndef sportandshowticket_h
#define sportandshowticket_h

#include <iostream>
#include <string>
#include <sstream>

//Class for showticket
    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
        std::string print_ticket(void){
            std::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;
    };

// Class for SportTicket


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
    std::string print_ticket(void){
        std::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();
    }
};


#endif /* sportandshowticket_h */

and a main file like this:

#include <iostream>
#include <sstream>
#include "sportandshowticket.h"
using namespace std;

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;
}

There are no errors shown in the code until compiling of which I receive this:

Ld /Users/danielreinoso/Library/Developer/Xcode/DerivedData/sportticket_test-fceknzfxtfescvbqfljgvbqtlofq/Build/Products/Debug/sportticket\ \ test normal (in target 'sportticket  test' from project 'sportticket  test')
    cd /Users/danielreinoso/Desktop/C  \ Course/sportticket\ \ test
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang   -target x86_64-apple-macos12.2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -L/Users/danielreinoso/Library/Developer/Xcode/DerivedData/sportticket_test-fceknzfxtfescvbqfljgvbqtlofq/Build/Products/Debug -F/Users/danielreinoso/Library/Developer/Xcode/DerivedData/sportticket_test-fceknzfxtfescvbqfljgvbqtlofq/Build/Products/Debug -filelist /Users/danielreinoso/Library/Developer/Xcode/DerivedData/sportticket_test-fceknzfxtfescvbqfljgvbqtlofq/Build/Intermediates.noindex/sportticket\ \ test.build/Debug/sportticket\ \ test.build/Objects-normal/x86_64/sportticket\ \ test.LinkFileList -Xlinker -object_path_lto -Xlinker /Users/danielreinoso/Library/Developer/Xcode/DerivedData/sportticket_test-fceknzfxtfescvbqfljgvbqtlofq/Build/Intermediates.noindex/sportticket\ \ test.build/Debug/sportticket\ \ test.build/Objects-normal/x86_64/sportticket\ \ test_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -Xlinker -no_adhoc_codesign -Xlinker -dependency_info -Xlinker /Users/danielreinoso/Library/Developer/Xcode/DerivedData/sportticket_test-fceknzfxtfescvbqfljgvbqtlofq/Build/Intermediates.noindex/sportticket\ \ test.build/Debug/sportticket\ \ test.build/Objects-normal/x86_64/sportticket\ \ test_dependency_info.dat -o /Users/danielreinoso/Library/Developer/Xcode/DerivedData/sportticket_test-fceknzfxtfescvbqfljgvbqtlofq/Build/Products/Debug/sportticket\ \ test

Undefined symbols for architecture x86_64:
  "ShowTicket::ShowTicket()", referenced from:
      SportTicket::SportTicket(char const*, char const*) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

How can I fix this?

CodePudding user response:

I guess the SportTicket constructor must call the super constructor with the appropriate arguments:

//Constructor
SportTicket(const char* RowSport, const char* SeatNumberSport):
ShowTicket(RowSport,SeatNumberSport),
beer_sold_check{false}
{}

PS you don't have to declare additional attributes RowSport and SeatNumberSport, just declare row and seat_number as protected instead of private, and they will be accessible from the SportTicket class.

  • Related