gpt4 book ai didi

c++ - 试图从类中调用函数

转载 作者:行者123 更新时间:2023-11-28 08:07:13 24 4
gpt4 key购买 nike

我正在尝试通过创建一个对象来执行 .h 文件中的代码。我做错了什么?

    //TicketFineCalculator.h
#include <iostream>
using namespace std;

class TicketFineCalculator
{

public:
int getFine() {
int procFee, zone, speedLimit, actualSpeed, totalFine;
int anotherRun = 1;
while (anotherRun == 1){
cout << "\n-------------------------------";
cout << "\nSpeeding Ticket Fine Calculator";
cout << "\n-------------------------------";
cout << "\nEnter processing fee, in dollars:";
cin >> procFee;
cout << "\nSpeeding Ticket #1";
cout << "\nEnter the type of speeding offense (1 for regular, 2 for work zone, 3 for residential district):";
cin >> zone;

cout << "\nEnter the speed limit, in miles per hour:";
cin >> speedLimit;
cout << "\nEnter the vehicle's speed, in miles per hour:";
cin >> actualSpeed;
cout << "\nThe total fine is:" << totalFine;
cout << "\nEnter 1 to enter process another speeding ticket or 0 to quit:";
cin >> anotherRun;
} // terminates while loop
return totalFine;
}
// Calculate the total fine given the road zone, speed limit, and the vehicle's actual speed.
// Return the fine as an integer.

};


//Project1.cpp
#include <iostream>
#include "TicketFineCalculator.h"

int totalFine;
TicketFineCalculator::getFine(totalFine);

int main(){
cout << totalFine;
return 0;
} //terminates main

最佳答案

如果您想在 TicketFineCalculator 中调用 getFine() 方法,您必须将方法声明为静态的,如下所示:

class TicketFineCalculator
{
public:
static getFine()
{
}
};

或者您必须创建 TicketFineCalculator 的实例并使用该实例调用该方法。

关于c++ - 试图从类中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10068806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com