未定义的符号:C++ 中的 Class::Class() 错误。我怎样才能解决这个问题?

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

我有一个包含两个 类 的头文件:

//
//  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 */

和这样的主文件:

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

在编译之前代码中没有显示错误,我收到了这个:

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)

我该如何解决这个问题?

我想 SportTicket 构造函数必须使用适当的参数调用超级构造函数:

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

PS你不必声明额外的属性RowSportSeatNumberSport,只需将rowseat_number声明为protected而不是 private,并且可以从 SportTicket class.

访问它们