2014-10-18 142 views
0

我想編譯這個cpp和h文件,但它一直給我這個錯誤「預期」('爲函數式的類型轉換或類型構造「和它指向的構造C++ Xcode預期'('用於函數式的類型轉換或類型構造

GasTank::GasTank(double a){ 
    capacity=a; 
} 

任何思考爲什麼,爲什麼它給我的錯誤,我不能弄明白這裏是代碼的其餘部分:。

// 
// Header.h 
// labs 
// 
// Created by Pxndroid on 10/17/14. 
// Copyright (c) 2014 Pxndroid. All rights reserved. 
// 

#include<string> 

using namespace std; 

class GasTank 
{ 
private: 
    double amount; 
    double capacity; 

public: 
    GasTank(double a); 
    void addGas(double b); 
    void useGas(double c); 
    bool isEmpty(); 
    bool isFull(); 
    double getGasLevel(); 
    double fillUp(); 

}; 

和:

// 
// main.cpp 
// labs 
// 
// Created by Pxndroid on 10/17/14. 
// Copyright (c) 2014 Pxndroid. All rights reserved. 
// 

#include <iostream> 
#include "Header.h" 

using namespace std; 

int main() 
{ 

    GasTank::GasTank(double a){ 
     capacity=a; 
    } 

    void GasTank::addGas(double b){ 
     if((amount+b)>capacity){ 
      amount=capacity; 
     } 
     else{ 
      amount+=b; 
     } 
    } 

    void GasTank::useGas(double c){ 
     if((amount-c)<0){ 
      amount=0; 
     } 
     else{ 
      amount-=c; 
     } 
    } 

    bool GasTank::isEmpty(){ 
     if(amount<0.1){ 
      return true; 
     } 
     else{ 
      return false; 
     } 
    } 

    bool GasTank::isFull(){ 
     if(amount>capacity-0.1){ 
      return true; 
     } 
     else{ 
      return false; 
     } 
    } 

    double GasTank::getGasLevel(){ 
     return amount; 
    } 

    double GasTank::fillUp(){ 
     capacity-=amount; 
     amount+=capacity; 

     return capacity; 
    } 
} 

回答

2

GasTank成員的定義移動到int main()之外。他們不屬於那裏,編譯器不期待他們;它們不是main()函數的一部分。

相關問題