C ++ Xcode期望'('用于函数样式的类型转换或类型构造 [英] c++ Xcode expected '(' for function-style cast or type construction

查看:157
本文介绍了C ++ Xcode期望'('用于函数样式的类型转换或类型构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编译此cpp和h文件,但它一直给我这个错误对于功能样式转换或类型构造,预期为'(',它指向构造函数

I'm trying to compile this cpp and h files but it keeps giving me this error "expected '(' for function-style cast or type construction" and it points to the constructor

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

有人在想为什么吗?我不知道为什么会给我这个错误。这是其余的代码:

Any thoughts why? I can't figure it out why it is giving me that error. Here is the rest of the code:

//
//  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();

};

a nd:

//
//  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;
    }
}


推荐答案

移动 int main()之外的 GasTank 成员的定义。它们不属于那里,并且编译器不希望它们存在。它们不是 main()函数的一部分。

Move the definitions of the GasTank members outside of int main(). They don't belong there, and the compiler is not expecting them; they are not part of the main() function.

这篇关于C ++ Xcode期望'('用于函数样式的类型转换或类型构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆