班级 |模块:总线问题

发布于 2025-01-09 01:24:17 字数 1978 浏览 0 评论 0原文

我是一名 C++ 初学者,正在使用类和模块。当我尝试运行该应用程序时,该应用程序给出以下错误:zsh:总线错误/Users/****/Documents/jacoProject/App。在互联网上我搜索了很多,但我发现了一些我不理解的东西,并且无法应用于我的代码。

我应该实现析构函数吗?如果是这样,我该如何在我的班级中实现它?预先感谢

func.cxx

module;
#include <string>

export module airline_ticket;
export class AirlineTicket
{

public:

  AirlineTicket();

  double calculatePriceInDollars();

  std::string getPassengerName();
  void setPassengerName(std::string name);

  int getNumberOfMiles();
  void setNumberOfMiles(int miles);

  bool hasEliteSuperRewardsStatus();
  void setHasEliteSuperRewardsStatus(bool status);

private:
  std::string q_passengerName;
  int q_numberOfMiles;
  bool q_hasEliteSuperRewardsStatus;

};

func_impl.cxx

module;
#include <string>
module airline_ticket;

AirlineTicket::AirlineTicket()
{
q_passengerName = "..not";
q_numberOfMiles = 0;
q_hasEliteSuperRewardsStatus = false;
}

double AirlineTicket::calculatePriceInDollars() 
{
  if (hasEliteSuperRewardsStatus())
  {
    return 0;
  }
  else
  {
    return getNumberOfMiles() * 0.1; 
  }
}


std::string AirlineTicket::getPassengerName() 
{
  return q_passengerName;
}


void AirlineTicket::setPassengerName(std::string name) 
{
  q_passengerName = name; 
}


int AirlineTicket::getNumberOfMiles() 
{
  return q_numberOfMiles;
}


void AirlineTicket::setNumberOfMiles(int miles) 
{
  q_numberOfMiles = miles; 
}


bool AirlineTicket::hasEliteSuperRewardsStatus() 
{
  return q_hasEliteSuperRewardsStatus;
}


void AirlineTicket::setHasEliteSuperRewardsStatus(bool status) 
{
  q_hasEliteSuperRewardsStatus = status;
}

main.cpp

#include <iostream>
import airline_ticket;

int main()
{
  AirlineTicket myTicket;
  myTicket.setPassengerName("Will SMith");
  myTicket.setNumberOfMiles(450);
  double ticketCost {myTicket.calculatePriceInDollars()};
  std::cout << ticketCost << std::endl;
  return 0;
}

I am a C ++ beginner and I am working with classes and modules. When I try to run the app, the app gives me the following error: zsh: bus error /Users/****/Documents/jacoProject/App. On the internet I searched a lot, but I found things that I didn't understand and that I couldn't apply to my code.

Should I implement a destructor? If so how can I implement it with my class? Thanks in advance

func.cxx

module;
#include <string>

export module airline_ticket;
export class AirlineTicket
{

public:

  AirlineTicket();

  double calculatePriceInDollars();

  std::string getPassengerName();
  void setPassengerName(std::string name);

  int getNumberOfMiles();
  void setNumberOfMiles(int miles);

  bool hasEliteSuperRewardsStatus();
  void setHasEliteSuperRewardsStatus(bool status);

private:
  std::string q_passengerName;
  int q_numberOfMiles;
  bool q_hasEliteSuperRewardsStatus;

};

func_impl.cxx

module;
#include <string>
module airline_ticket;

AirlineTicket::AirlineTicket()
{
q_passengerName = "..not";
q_numberOfMiles = 0;
q_hasEliteSuperRewardsStatus = false;
}

double AirlineTicket::calculatePriceInDollars() 
{
  if (hasEliteSuperRewardsStatus())
  {
    return 0;
  }
  else
  {
    return getNumberOfMiles() * 0.1; 
  }
}


std::string AirlineTicket::getPassengerName() 
{
  return q_passengerName;
}


void AirlineTicket::setPassengerName(std::string name) 
{
  q_passengerName = name; 
}


int AirlineTicket::getNumberOfMiles() 
{
  return q_numberOfMiles;
}


void AirlineTicket::setNumberOfMiles(int miles) 
{
  q_numberOfMiles = miles; 
}


bool AirlineTicket::hasEliteSuperRewardsStatus() 
{
  return q_hasEliteSuperRewardsStatus;
}


void AirlineTicket::setHasEliteSuperRewardsStatus(bool status) 
{
  q_hasEliteSuperRewardsStatus = status;
}

main.cpp

#include <iostream>
import airline_ticket;

int main()
{
  AirlineTicket myTicket;
  myTicket.setPassengerName("Will SMith");
  myTicket.setNumberOfMiles(450);
  double ticketCost {myTicket.calculatePriceInDollars()};
  std::cout << ticketCost << std::endl;
  return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文