2012-04-09 92 views
0

调用函数我试图通过创建一个对象执行.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 
+2

你在哪里创建了一个对象? &为什么冒险与*内联*功能? – 2012-04-09 04:15:46

+0

TicketFineCalculator :: getFine(totalFine); – CryptoJones 2012-04-09 04:16:45

+2

你正在尝试*调用'main()'之外的函数吗?你不能这么做,你应该在'main()'中调用它。 – 2012-04-09 04:17:36

回答

1

如果你想打电话内TicketFineCalculator的getFine()方法时,必须声明静态方法,如下所示:

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

,或者你必须创建TicketFineCalculator的一个实例,并调用使用该方法实例。

+0

你的意思是在Project1内调用它吗?如果是这样,我该如何创建一个类的实例?那是我以为我做的。 – CryptoJones 2012-04-09 04:21:40