2012-04-09 85 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